如何在 PHP 登录功能中添加用户访问级别
<p>我开发了一个 PHP 登录/注册脚本,当用户登录时,它会启动一个会话并创建一个 cookie,然后使用以下命令将用户重定向到仪表板.php:</p>
<pre class="brush:php;toolbar:false;">//get data in variables
$emailAddress = mysqli_real_escape_string($conn,$_POST['emailAddress']);
$password = mysqli_real_escape_string($conn,$_POST['password']);
$remember = isset($_POST['rememberMe']);
if(!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
$error = 'Please enter a valid email address.';
}
elseif(email_exists($emailAddress,$conn)) { // if email address exists
$result = mysqli_query($conn,"SELECT password FROM users WHERE email = '$emailAddress'");
$retrievePassword = mysqli_fetch_assoc($result);
if(!password_verify($password,$retrievePassword['password'])) // if password does not match
{
$error = 'Password is incorrect.';
} else { // if password correct, log user in
$_SESSION['email'] = $emailAddress;
if($remember == 'on') { // if "keep user logged in" was ticked
setcookie("email",$emailAddress,time()+7200); // keep user logged in for 2 hours
}
header("location: dashboard.php");
}
} else { // if email does not exist
$error = 'Email address not registered.';
}</pre>
<p>在我所有受限页面(例如dashboard.php)的顶部,我有:</p>
<pre class="brush:php;toolbar:false;">if(logged_in()) { // if user logged in, show page</pre>
<p>logged_in() 函数是:</p>
<pre class="brush:php;toolbar:false;">function logged_in(){
if(isset($_SESSION['email']) || isset($_COOKIE['email'])) {
return true;
} else {
return false;
}
}</pre>
<p>在数据库中,我有一个标题为 <code>usrUserTypeNo</code> 的列,其中管理员为“1”,普通用户为“3”。
如何将此用户类型合并到会话中,以便我可以根据用户的角色确定向用户显示哪些内容?</p>