2017-07-26 129 views
2

我有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> 

預先感謝您!

+0

如果($ ACCESS_LEVEL == 0){ 標題(「地點:管理員/home.php「); } 「0」表示FALSE意思是$ access_level沒有值 – Shefali

+0

實際上它驗證用戶的訪問級別。在我的數據庫中,管理員帳戶的訪問級別爲0,用戶1等等。所以我認爲這部分沒有問題。 :) –

回答

0

頁的頂部這一個代碼。把相關的代碼在每一頁的頂部像

管理頁面

<?php 
session_start(); 
if($_SESSION['type'] != 0){ 
     echo "Unauthorized user. Access denied." 
     die; // stop further execution 
} ?> 

用戶頁面

<?php 
session_start(); 
if($_SESSION['type'] != 1){ 
     echo "Unauthorized user. Access denied." 
     die; // stop further execution 
    } ?> 
+0

如何索引,我需要有一個會話在那一個?因爲我試圖將其添加到管理頁面上,並且在登錄時只顯示「未經授權的用戶。拒絕訪問」。信息。 :( –

+0

我已經編輯了我的答案檢查現在,我已經從條件中刪除了額外的'=',這是嚴格的類型 –

+0

哇!它真的有效,非常感謝! –

-1

使用你必須在每一頁上檢查會議的所有

<?php if($_SESSION['type']==1){ 
     header('index.php?msg=Admin Not Access This page'); 
    }?> 
+0

'類型'是否屬於訪問級別? –

+0

你已經在用戶註冊時間添加了一些類型或角色,所以使用它 –