简体   繁体   English

PHP 中的逻辑或运算符

[英]Logical OR Operator in PHP

In my webpage I have 2 roles 'User' and 'Admin'.在我的网页中,我有 2 个角色“用户”和“管理员”。 Admins have access to Create, Read, Update, and Delete.管理员有权创建、读取、更新和删除。 Users have access to Create, but can also see short parts of the record on the overview table.用户可以访问创建,但也可以在概览表上查看记录的短部分。

if ($_SESSION['userRole'] !== 'ADMIN') {
    header("Location:index.php");
    exit;
}

This is what i use normally before the change i want to verify the role in the edit.php.这是我在更改之前通常使用的,我想在edit.php 中验证角色。 And i have also tried this to allow Users to edit as well.而且我也尝试过允许用户进行编辑。

if ($_SESSION['userRole'] !== 'ADMIN' or !== 'USER') {
    header("Location:index.php");
    exit;
} 
//////////////////////////////////////////////////////////////
if ($_SESSION['userRole'] !== 'ADMIN' || !=='USER') {
    header("Location:index.php");
    exit;
}
//////////////////////////////////////////////////////////////
if ($_SESSION['userRole'] !== 'ADMIN') {
    header("Location:index.php");
    exit;
}elseif ($_SESSION['userRole'] !== 'USER') {
    header("Location:index.php");
    exit;
}

When pressing the Edit button.按下编辑按钮时。 The page doesn't do anything.该页面不执行任何操作。 This happens on Admin and User这发生在管理员和用户

First, i'll think you should work with integers instead of strings like首先,我认为你应该使用整数而不是字符串

$_SESSION["role"] = 666; //Admin
$_SESSION["role"]  = 1;  //User

in your login.在您的登录中。

if($_SESSION["role"] === 666){

  // redirect to the admin-page

}elseif($_SESSION["role"] === 1){

  // redirect to users-page  

}else{

  //redirect your "both" page

}

Try to check if the redirect page exists or throw an error if not.尝试检查重定向页面是否存在,如果不存在则抛出错误。

Good luck to you祝你好运

try this source, I think errors will come from "header already sent"试试这个来源,我认为错误将来自“标头已发送”

if ($_SESSION['userRole'] !== 'ADMIN' OR $_SESSION['userRole'] !== 'USER') {
    echo '<script> window.location.replace("https://www.example.com"); </script>';
    die();
} 
//////////////////////////////////////////////////////////////
if ($_SESSION['userRole'] !== 'ADMIN' || $_SESSION['userRole'] !=='USER') {
    echo '<script> window.location.replace("https://www.example.com"); </script>';
    die();
}
//////////////////////////////////////////////////////////////
if ($_SESSION['userRole'] !== 'ADMIN') {
echo '<script> window.location.replace("https://www.example.com"); </script>';
    die();
}elseif ($_SESSION['userRole'] !== 'USER') {
    echo '<script> window.location.replace("https://www.example.com"); </script>';
    die();
}

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

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