我試圖瞭解如何建立一個用戶註冊與PHP和MySQL。PHP註冊與會話和用戶訪問
我已經構建了一個表格,用戶可以填寫並將信息存儲在我的表格中。
error_reporting(E_ALL);
include_once ('connection.php');
// Required field names
$required = array('firstname', 'lastname', 'email', 'password', 'accounttype');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
echo "All fields are required.";
} else {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$password = md5($_POST['password']);
$accounttype = $_POST['accounttype'];
$query = "INSERT INTO users(firstname,lastname,email,password,accounttype) VALUES (:firstname,:lastname,:email,:password,:accounttype)";
$stmt = $dbh->prepare($query);
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':password', $password);
$stmt->bindParam(':accounttype', $accounttype);
$stmt->execute();
if(!$query){
echo 'Whoops, something went wrong!';
} else {
echo $accounttype;
if($accounttype == '1'){
header ('Location: /england/dashboard.php');
exit;
};
if($accounttype == '2'){
header ('Location: /ireland/dashboard.php');
exit;
};
};
};
當用戶完成表單時,他們根據其帳戶類型被重新分配到不同的頁面。
在這些頁面上,我需要以某種方式檢查用戶是否屬於accounttype'X'。所以,如果他們在
header ('Location: /ireland/dashboard.php');
登陸他們的賬戶類型值將等於2,因此只有與人的賬戶類型2可以訪問上面提到的。
我讀過關於會話變量,但我在哪裏設置這些?
請參閱http://php.net/session.examples.basic – mario