我试图了解如何使用 PHP 和 MySQL 构建用户注册。
我已经建立了一个用户可以填写的表格,然后将信息存储在我的表格中。
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;
};
};
};
当用户完成表单时,他们会根据他们的帐户类型被重定向到不同的页面。
在这些页面上,我需要以某种方式检查用户是否属于帐户类型“X”。所以如果他们登陆
header ('Location: /ireland/dashboard.php');
他们的帐户类型值将等于 2,因此只有帐户类型为 2 的人才能访问上述内容。
我已经阅读了有关会话变量的信息,但是我在哪里设置这些?