如何在 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>