Visitor / user permissions

0

Generate the following comparison :

   <?php
      if($_SESSION['id_nivel'] == 1 || 2){
        ?>
          <div class="col-md-6">
            <select name="cantidad" class="form-control">
    <option class="form-control" value="1">1</option>
    <option class="form-control" value="2">2</option>
    <option class="form-control" value="3">3</option>
    <option class="form-control" value="4">4</option>
    <option class="form-control" value="5">5</option>
          </select>
      </div>
          <input name="id_producto" type="hidden" value="<?php echo $columnas["id_producto"];?>">
          <div class="col-md-6" id="carrito">
         <button type="submit" id="boton-add" class="btn bg-lim white" alt="Agregar al carrito" title="Agregar al carrito"> 
           Añadir al carrito <i class="fa fa-shopping-cart" aria-hidden="true"></i> 
        </button>
      </div>
      <?php 
    } else{
       echo '<div class"col-md-6 col-xs-6 col-sm-6 col-6 col-lg-6 col-lx-6>
      <a data-toggle="modal" data-target="#createacc">Crear Cuenta</a> o 
       <a data-toggle="modal" data-target="#login">Iniciar Sesión</a>       
     </div>';
    }
      ?>

But it generates the following notice :

Notice: Undefined index: id_nivel

I understand that it is because a id_level is not set to $ _ SESSION because it is not registered, that is, when creating a user, id_user and id_level then, you can generate the verification of both parties.

  

Clarification : I thought of this method of hiding the button: Add to the Cart, because when a level_id or user is not registered, when you click Add to Cart generates missing errors of $ variables, etc.

My question :

How do I detect if a $ _ SESSION has or not id_level?

That is:

id_nivel = 1 (Administrator) || 2 (User)

Levelless: Visit;

    
asked by Juan 01.02.2018 в 15:58
source

2 answers

1

I was able to solve it with isset()

I leave the code:

      <?php
    if(isset($_SESSION['id_nivel'])){
        ?>
    <form>
      <div class="btn-group cart ">
        <div class="form-group col-md-12">
          <div class="col-md-6">
            <select name="cantidad" class="form-control">
    <option class="form-control" value="1">1</option>
    <option class="form-control" value="2">2</option>
    <option class="form-control" value="3">3</option>
    <option class="form-control" value="4">4</option>
    <option class="form-control" value="5">5</option>
          </select>
      </div>
  <input name="id_producto" type="hidden" value="<?php echo $columnas["id_producto"];?>">
          <div class="col-md-6" id="carrito">
        <button type="submit" id="boton-add" class="btn bg-lim white" alt="Agregar al carrito" title="Agregar al carrito">
          Añadir al carrito <i class="fa fa-shopping-cart" aria-hidden="true"></i> 
        </button>
      </div>
      </div>
      </div>
      </form>

      <?php 
      }
    else {
      echo '<div class"col-md-6 col-xs-6 col-sm-6 col-6 col-lg-6 col-lx-6>Debes estar <a style="cursor:pointer;" data-toggle="modal" data-target="#createacc">registrado</a> para comprar. ¿Ya tienes una cuenta? <a style="cursor:pointer;" data-toggle="modal" data-target="#login">Iniciar Sesión</a></div>';
        }
      ?>

Solved this way since the visitor to enter does not set any id_nivel (only to users) then to not have id_nivel executes the else

    
answered by 02.02.2018 / 18:21
source
0

You could do this by registering the visitor, if you need more parameters you should add them.

session_start();
$_SESSION["id_nivel"] = 3; 

Luego validas así   

if(isset($_SESSION['id_nivel'])) //Validar si existe la sesión
    {
       //Si existe la sesión
        if($_SESSION['id_nivel'] == 1 || 2)
        {
           //Es usuario o admin
        }
        else{
           //Es visitante
        }
    }
    else{
        /*
         * No existe sesión y por lo tanto no puedes
         * obtener el el parámetro id_nivel
         * a menos que envíes al session_start
         * y registres los parámetros que un usuario
         * al estar conectado necesita
         */         

    }
    
answered by 01.02.2018 в 18:00