检查用户权限

dphi5xsq  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(288)

我想通过mysql中的手动设置值来检查用户是否有权限在登录过程中查看站点。
如何将该支票插入此代码:

include 'dbh.inc.php';

$uid = mysqli_real_escape_string($conn, $_POST['uid']);
$pwd = mysqli_real_escape_string($conn, $_POST['pwd']);

//Error handlers
//check if inputs are empty
if (empty($uid) || empty($pwd)) {
    header("Location: ../index.php?login=empty");
    exit();
} else {
    $sql = "SELECT * FROM users WHERE user_uid='$uid' OR user_email='$uid'";
    $result = mysqli_query($conn, $sql);
    $resultCheck = mysqli_num_rows($result);
    if ($resultCheck < 1) {
        header("Location: ../index.php?login=error");
        exit();
    } else {
        if ($row = mysqli_fetch_assoc($result)) {
            //de-hash pass
            $hashedPwdCheck = password_verify($pwd, $row['user_pwd']);
            if ($hashedPwdCheck == false) {
                header("Location: ../index.php?login=error");
                exit();
            } elseif ($hashedPwdCheck == true) {
                //log in user here
                $_SESSION['u_id'] = $row['user_id'];
                $_SESSION['u_first'] = $row['user_first'];
                $_SESSION['u_last'] = $row['user_last'];
                $_SESSION['u_email'] = $row['user_email'];
                $_SESSION['u_uid'] = $row['user_uid'];
                header("Location: ../index.php?login=success");
                exit();
            }
        }
    }
}
tyu7yeag

tyu7yeag1#

你已经做了很多事情了。但你的问题还不够清楚。您想阻止用户登录还是只允许用户有限地访问某些页面?

if ($hashedPwdCheck == false) {
 header("Location: ../index.php?login=error");
 exit();
} elseif ($hashedPwdCheck == true) {
 if($row['user_can_login']){ 
  //log in user here
  $_SESSION['u_id'] = $row['user_id'];
  $_SESSION['u_first'] = $row['user_first'];
  $_SESSION['u_last'] = $row['user_last'];
  $_SESSION['u_email'] = $row['user_email'];
  $_SESSION['u_uid'] = $row['user_uid'];
  header("Location: ../index.php?login=success");
  exit();
 }else{
  header("Location: ../index.php?login=error");
  exit();
 }
}

这是为了阻止现有用户登录。

相关问题