简体   繁体   English

我正在尝试检查用户是管理员还是标准用户

[英]I am trying to check whether the user is an admin or a standard user

When a user logs in to the site, I want it to run a check on whether or not they are an admin. 当用户登录该站点时,我希望它检查他们是否是管理员。 If they are an admin I would like to send them to the admin page and if they are a user then send them to the homepage. 如果他们是管理员,我想将他们发送到管理页面,如果他们是用户,则将他们发送到主页。 However I am having a few issues. 但是我遇到了一些问题。 The errors that have come up are 出现的错误是

"Syntax Error, unexpected == (T_IS_EQUAL), expecting ',' or ')'". “语法错误,意外==(T_IS_EQUAL),期待','或')'”。

I am trying this using xampp. 我正在尝试使用xampp。 Admin is the row on the database which would store the integers either 1 or 2. 1 being a standard user and 2 being the admin. Admin是数据库中的行,它将integers存储为1或2. 1是标准用户,2是admin。

if(!isset($_SESSION['admin'] == '1' OR $_SESSION['admin'] == '')){
    header('Location: home.php');
} else if(!isset($_SESSION['admin'] === '2')){
    header('Location: admin.php');
}

I expect that when a standard user logs in to the website that it should redirect them to the homepage, however when an admin logs in then it should redirect them to the admin page. 我希望当标准用户登录到网站时它应该将它们重定向到主页,但是当管理员登录时,它应该将它们重定向到管理页面。

You are trying to check if a condition isset, but isset (in docs) determine if a variable is declared and is different than NULL 您正在尝试检查条件是否已设置,但isset (在文档中)确定是否声明了变量并且不是NULL

You should check like this: 你应该像这样检查:

if (isset($_SESSION['admin'])) {
    if ($_SESSION['admin'] === "2") {
        header('Location: admin.php');
    } else {
        header('Location: home.php');
    }
} else {
    header('Location: home.php');
}

You can do this from Post Login file directly by checking rows of the database and giving appropriate header location . 您可以通过检查数据库的行并提供适当的标题位置,直接从Post Login文件执行此操作。 See the below code of mine 请参阅下面的代码

if($rows['registrationType'] == 1) //for user
        {
            $_SESSION['user']['id'] = $rows['userId'];//Set Sessions
            header('location: user/index.php');
        }
        elseif($rows['registrationType'] == 2) // for admin
        {
            $_SESSION['admin']['id'] = $rows['userId'];//Set Sessions

            /*Header Location Redirects to that page*/
            header('location: admin/index.php');

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM