我想讓登錄處理器與用戶輸入的內容匹配,包含名字,姓氏和硬編碼對象數組密碼。我不知道如何在登錄和對象數組之間進行檢查。我有3頁,student.class.php,login.php和processor.php。
我還需要使用session_start()和$ _SESSION數組,但我不知道如何將其實現到我的項目中。我會製作一個單獨的會話文件,還是將其包含到我的處理器中?
Student.class.php
<?php
class Student
{
private $f_name;
private $l_name;
private $full_name;
private $password;
//Constructor method
public function __construct($f_name,$l_name,$password) {
$this->f_name = $f_name;
$this->l_name = $l_name;
$this->password = $password;
}
function get_name(){
$full_name = $this->f_name.' '.$this->l_name;
return $full_name;
}
function get_level(){
return $this->level;
}
function get_gender(){
return $this->gender;
}
function get_password(){
return $this->password;
}
}
?>
的login.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
require_once 'student.class.php';
$students = array();
$students[0] = new Student('Trey', 'Smith', 'senior', 'male', 'sailor1234');
$students[1] = new Student('Kyle', 'McAulay', 'junior', 'male', 'abc123');
$students[2] = new Student('Stacey', 'Keibler','senior','female', 'hotdawg23');
$students[3] = new Student('Lindsey', 'Mullins', 'junior','female','gonoles69');
$students[4] = new Student('Kenneth', 'Jaggers','senior', 'male', 'peterpanpan');
$students[5] = new Student('Chad', 'Endris', 'sophomore', 'male','back2thefuture');
?>
<h1>Sign up for our site!</h1>
<form method="post" action="processor.php">
<fieldset>
<label>First Name</label>
<input type="text" name="first_name"/>
<label>Last Name</label>
<input type="text" name="last_name"/>
<label>Password</label>
<input type="text" name="password"/>
</fieldset>
<br><input type="submit" value="Submit"></input></br>
</body>
</html>
processor.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
require_once 'student.class.php';
require_once 'login.php';
function check_submit($field_to_check)
{
if (isset($_POST[$field_to_check]) && $_POST[$field_to_check] != '')
{
return TRUE;
}
else
{
return "Please fill in the $field_to_check category!";
}
}
$errors = array();
$_POST['first_name'] = strip_tags(trim($_POST['first_name']));
$_POST['last_name'] = strip_tags(trim($_POST['last_name']));
$_POST['password'] = strip_tags(trim($_POST['password']));
if (check_submit('first_name') !== TRUE)
{
$errors[] = check_submit('first_name');
}
if (check_submit('last_name') !== TRUE)
{
$errors[] = check_submit('last_name');
}
if (check_submit('password') !== TRUE)
{
$errors[] = check_submit('password');
}
if (count($errors)> 0){
echo "<ul>";
foreach($errors as $message){
echo "<li>$message</li>";
}
echo "</ul>";
die();
}
?>
<h1>Thank you for registering <?php echo $_POST['first_name']."
".$_POST['last_name']?>!</h1>
<h2>Your password was correct!</h2>
</body>
</html>
我剛纔注意到f_name,l_name等都是私有的。這是必要的嗎?如果是這樣,您必須將它們從對象中傳遞出來,或者將登錄檢查器作爲類中的一種方法。 –