PHP多用户登录会话

问题描述:

我有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等等。所以我认为这部分没有问题。 :) –

页的顶部这一个代码。把相关的代码在每一页的顶部像

管理页面

<?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

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

使用你必须在每一页上检查会议的所有

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

'类型'是否属于访问级别? –

+0

你已经在用户注册时间添加了一些类型或角色,所以使用它 –