【问题标题】:Check user permission检查用户权限
【发布时间】:2018-09-28 02:34:46
【问题描述】:

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

【问题讨论】:

  • if (!$row['u_can_view_site']) { header('Location: http://blueballmachine2.ytmnd.com/'); exit; }

标签: php mysql session


【解决方案1】:

您已经完成了几乎所有工作。但是你的问题不够清楚。您想阻止用户登录还是只允许用户有限地访问某些页面?

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

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

【讨论】:

  • 您应该使用 cmets (Not answers) 来请求澄清。
  • @FrankerZ 对不起,我没有足够的代表。但我确实建议了一些答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-27
  • 2013-05-04
  • 1970-01-01
  • 2018-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多