PHP IF if two conditions are met

0

I want a drop-down menu only to be able to see the links to the sites to which you have permission. For this I apply a variable with the function SESSION and if it is one level or another I indicate that you can see or not see the menu.

Right now I have two examples but none of them works for me, I can only make it work by applying a single condition in the IF but of course, I need several at a time because I have 5 user levels.

<?php
          if (!isset($_SESSION["privilegio"]) || $_SESSION["privilegio"] == 5 and $_SESSION["privilegio"] == 3) {
            echo '<li><a href="../categoria/index.php"><i class="fa fa-circle-o"></i> Tabla Categorías</a></li>';
            echo '<li><a href="../subcategoria/index.php"><i class="fa fa-circle-o"></i> Tabla Subcategorías</a></li>';
            echo '<li><a href="../tipodocumentos/index.php"><i class="fa fa-circle-o"></i> Tabla Tipo de Documentos</a></li>';
          }
        ?>      

And this is the other:

<?php
          if (!isset($_SESSION["privilegio"]) || $_SESSION["privilegio"] == 5 and == 3) {
            echo '<li><a href="../categoria/index.php"><i class="fa fa-circle-o"></i> Tabla Categorías</a></li>';
            echo '<li><a href="../subcategoria/index.php"><i class="fa fa-circle-o"></i> Tabla Subcategorías</a></li>';
            echo '<li><a href="../tipodocumentos/index.php"><i class="fa fa-circle-o"></i> Tabla Tipo de Documentos</a></li>';
          }
        ?>                                              
    
asked by Alberto Cepero de Andrés 07.07.2017 в 08:34
source

2 answers

2

Try using the same type of operators, if you use "and" you should use "or" and if you prefer to use "||" you should use "& &".

I do not know exactly the difference, but I know there is. For more information php operators preferences

I would also advise you to use "(" and ")" to me at the beginning it helped me to clarify what conditions were executed first, and that they did it as I wanted.

Anyway, I do not find much logic, you are indicating that privilege == 5 and with an "and" you are saying that it has to be == 3. I do not think this is ever fulfilled. (Without parenteresis & & & & & & & & commat; & will have preference over the || so it first compares it to be == 5 Y == 3

if (!isset($_SESSION["privilegio"]) || $_SESSION["privilegio"] == 5 && $_SESSION["privilegio"] == 3)

Try with:

if (isset($_SESSION["privilegio"]) && ($_SESSION["privilegio"] == 5) || $_SESSION["privilegio"] == 3))

With this first you check with isset that this variable is declared, and then it is one of the following permissions.

Greetings. I hope it serves you.

    
answered by 07.07.2017 / 08:51
source
2
if (isset($_SESSION["privilegio"]) && $_SESSION["privilegio"] == 5 || $_SESSION["privilegio"] == 4) 

The user aldanux almost did it well, but it was the & because the condition does not have to be given at the same time if a user enters with us nivle not with two, then the solution was change these & by | .

    
answered by 07.07.2017 в 08:48