简体   繁体   中英

PHP redirect issue with the user of OR ||

I am forever redirected to access denied.php no matter what.. My user role is a user and my echo says it is a user as well..

  elseif (($_SESSION['role'] != 'user' || $_SESSION['role'] != 'admin'))
    {
    echo("<script>alert('You are not admin')</script>");
    header ("Location: access_denied.php");}

anyone know what's wrong?

You need to be using AND since only one condition needs to be false for the statement to fail. It appears that you want both to fail for this code to be executed:

elseif (($_SESSION['role'] != 'user' && $_SESSION['role'] != 'admin'))

From php.net :

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

As I can see in your script you are echo-ing output before the header(). This might be a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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