我有7個用戶級別。我將被重定向取決於我的輸入(例如我輸入管理員的憑據,我將被重定向到管理頁面),同樣與其他6.我有的問題是,成功登錄後,如果我改變從(localhost/admin/home.php)到(localhost/employee/home.php)的URL我現在可以訪問員工的頁面。我想對此有限制。或者可能是「未經授權的用戶訪問被拒絕」的錯誤。類似的東西。這是我的代碼。PHP多用戶登錄會話
的index.php
<form action="checklog.php" method="POST">
<h1>Log in</h1>
<p>
<label for="username" class="uname" > Your email or username </label>
<input id="username" name="username" required="required" type="text" placeholder="myusername " minlength="2" />
</p>
<p>
<label for="password" class="youpasswd"> Your password </label>
<input id="password" name="password" required="required" type="password" placeholder="eg. X8df!90EO" minlength="2" />
</p>
<input type="submit" name="submit" value="Login">
</form>
<?php // To display Error messages
if(isset($_GET['err'])){
if ($_GET['err']==1){
echo "Invalid Credentials.";}
else if($_GET['err']==5){
echo "Successfully Logged out";}
else if ($_GET['err']==2){
echo "You're trying to access an unauthorized page.";
}
}
?>
</body>
checklog.php(這是我處理的憑據。)
<?php
require_once("db.php");
function check_input($r){
$r=trim($r);
$r=strip_tags($r);
$r=stripslashes($r);
$r=htmlentities($r);
$r=mysql_real_escape_string($r);
return $r;
}
if (isset($_POST['username'],$_POST['password'])){
$u=check_input($_POST['username']);
$p=md5(check_input($_POST['password']));
try{
$db=get_db();
$stmt=$db->prepare("SELECT * FROM users WHERE username=? && password=?");
$stmt->execute(array($u,$p));
$r=$stmt->fetch(PDO::FETCH_ASSOC);
if($r){
session_start();
$access_level=$r['access_level'];
$_SESSION['username']=$r['username'];
$_SESSION['access_level']=$access_level;
if ($access_level==0){
header("Location: admin/home.php");
}
if($access_level==1){
header("Location: user/home.php");
}
if($access_level==2){
header("Location: businesshead/home.php");
}
if($access_level==3){
header("Location: scm/home.php");
}
if($access_level==4){
header("Location: finance/home.php");
}
if($access_level==5){
header("Location: gm/home.php");
}
if($access_level==6){
header("Location: scma/home.php");
}
}
else{
header("Location:index.php?err=1");
}
}
catch(PDOException $e){
die("Database error: ".$e->getMessage());
}
}
else{
header("Location:index.php");
}
?>
並讓姑且認爲這是我的管理頁面(admin.php的)
<!DOCTYPE html>
<body>
Welcome!
</body>
</html>
預先感謝您!
如果($ ACCESS_LEVEL == 0){ 標題(「地點:管理員/home.php「); } 「0」表示FALSE意思是$ access_level沒有值 – Shefali
實際上它驗證用戶的訪問級別。在我的數據庫中,管理員帳戶的訪問級別爲0,用戶1等等。所以我認爲這部分沒有問題。 :) –