检查用户权限

时间:2018-09-28 02:34:46

标签: php mysql session

我想通过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();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您已经完成了几乎所有的工作。但是您的问题还不够清楚。您是否要阻止用户登录或仅允许该用户限制访问某些页面?

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();
 }
}

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