使用Access用户级别登录PHP

时间:2018-08-07 08:44:14

标签: php

我要使用访问用户级别和状态(如果用户处于活动状态或非活动状态)登录页面 check.php页面

localStorage

当用户输入正确的密码时我的代码工作正常,但是如果用户输入错误的密码不起作用,则卡在check.php页面上。如果出现错误,请更正我的代码

2 个答案:

答案 0 :(得分:0)

我认为您需要将else移到if($ r ['status'] =='1')块之外。

if($r){
    if($r['status']=='1'){
        session_start();
        $access_level=$r['access_level'];
        $_SESSION['emp_id']=$r['emp_id'];
        $_SESSION['access_level']=$access_level;

        if ($access_level==0){
            header("Location:emppages/");
        }
        if($access_level==1){
            header("Location:hrpages/");
        }
        if($access_level==2){
            header("Location:adpages/");
        }
    }
    else{
        header("Location:index.php?err=4");
    }
} else{
    header("Location:index.php?err=1");
}

答案 1 :(得分:0)

需要考虑的一些事情。不是直接解决问题的方法,但是它可以帮助您清除问题,最有可能解决问题或使其更容易找到。

进行标头重定向(有时是绝对必要的)后退出是个好习惯

header("Location: /something"); 
exit;

为什么不使用PHP的password_hash()/ password_verify?容易得多。

创建用户:

$username = isset($_POST['username']) ? trim($_POST['username']) : null;
$password = isset($_POST['password']) ? trim($_POST['password']) : null;

if(!empty($username) && !empty($password)){

    $hash = password_hash($password, PASSWORD_DEFAULT); 
   // Store in Database etc...

}

登录用户:

$username = isset($_POST['username']) ? trim($_POST['username']) : null;
$password = isset($_POST['password']) ? trim($_POST['password']) : null;

if(!empty($username) && !empty($password)){

    // Do DB query getting the username's password

    $hashedPassword = $QueryResult->password;

    if(password_verify($password, $hashedPassword){
        // Success - check user level and status etc
    }else{
        // Failed
    }

}