【问题标题】:How to redirect two users according to their roles in PHP如何根据他们在PHP中的角色重定向两个用户
【发布时间】:2018-01-05 15:33:43
【问题描述】:

如何根据角色重定向两个用户?我有数据库用户类型 1 和类型 0。我的登录代码是这样的。类型 1 假设是管理员,类型 0 是普通用户。

<?php

session_start();

if(isset($_POST['submit'])){

include 'dbh.inc.php';

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


if (empty($uid) || empty($pwd)){
    header("Location: ../index.php?login=empty");
    exit();
} else {
    $sql = "SELECT * FROM users WHERE user_uid='$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)){
            $hashedPwdCheck = password_verify($pwd, $row['user_pwd']);
            if($hashedPwdCheck == false){
                header("Location: ../index.php?login=error");
                exit();
            } elseif ($hashedPwdCheck == true) {
                $_SESSION['u_id'] = $row['user_id'];
                $_SESSION['u_first'] = $row['user_first'];
                $_SESSION['u_last'] = $row['user_last'];
                $_SESSION['u_uid'] = $row['user_uid'];
                header("Location: ../main.php?login=success");
                exit();
            }   
        }
    }
}
} else {
header("Location: ../index.php?login=error");
exit();
}

【问题讨论】:

  • 对于用户类型 1 和类型 0 ,创建一个 if else 标头条件,例如 header("Location: ../main.php?login=success"); to if(type==1){ header("位置:../main.php?login=success");}elseif(type==0){ header("位置:../mainuser.php?login=成功");}
  • 那么您是否打算将管理员重定向到一个页面并将普通用户重定向到另一个页面?
  • 是存储在 users 表中的用户类型。我没有看到这个字段?
  • 是什么阻止我进入 main.php 并绕过身份验证?
  • 是的管理员有不同的页面,用户有另一个页面,我有两个用户类型 1 和类型 0.....请编辑我的代码,我真的不知道如何编码我只是从网上复制粘贴过来的。

标签: php database types


【解决方案1】:

你可以通过if()..else...轻松做到这一点,但千万不要直接使用header来重定向,我建议你使用这个函数来重定向:

function redirect($url){
    if (headers_sent()){
        die('<script type="text/javascript">window.location.href=\'' . $url . '\';</script>');
    }else{
        header('Location: ' . $url);
      die();
    }
}

这是你需要做的:

if($type == 1)
    redirect($admin_url);
else
    redirect($user_url);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 2017-11-29
    • 1970-01-01
    相关资源
    最近更新 更多