If not working correctly - PHP

1

Why does not the second if work? When doing echo and comment that if the result of it is 1 (in the case where I am) and if it is 1 or 2 should not redirect me but if it does, does anyone see the failure?

<?php
    session_start();
    include ("db_files/db.php");
    if (!isset($_SESSION['user'])){ header("Location: index.php"); }
    echo $_SESSION['userlevel'];
    if ($_SESSION['userlevel'] != 1 || $_SESSION['userlevel'] != 2) { header ("location: mainindex.php");}
?>
    
asked by Pavlo B. 08.11.2016 в 18:02
source

1 answer

5

Is that if it's 1 or 2 ... it gives you one or the other true and redirects you.

You can solve it in this simpler way:

if ($_SESSION['userlevel'] > 2) {

  header ("location: mainindex.php");
}

or with your code you compare it with the operated && :

if ($_SESSION['userlevel'] != 1 && $_SESSION['userlevel'] != 2) {

  header ("location: mainindex.php");
}
    
answered by 08.11.2016 / 18:13
source