targeting users validated by php

-1

Good afternoon I did a system, bd ok and loguin check ok but I wanted it after checking the user it opened the right page for the right user example: I have 3 levels being them 0 1 2 and I want the level 0 user to open the page index1.php or user level 1 open the page index2.php and the user level 2 open the page index3.php

<?php

session_start(); // Inicia a session

include "config.php";

$usuario = $_POST['usuario'];
$senha = $_POST['senha'];

if ((!$usuario) || (!$senha)){

  echo "Por favor, todos campos devem ser preenchidos! <br /><br />";

  include "index.html";

}else{

  $senha = md5($senha);

  $sql = mysql_query(

    "SELECT * FROM xxxxxx_usuarios
    WHERE usuario='{$usuario}'
    AND senha='{$senha}'
    AND ativado='1'"

  );

  $login_check = mysql_num_rows($sql);

  if ($login_check > 0){

    while ($row = mysql_fetch_array($sql)){

      foreach ($row AS $key => $val){

        $$key = stripslashes( $val );

      }

      $_SESSION['usuario_id'] = $usuario_id;
      $_SESSION['nome'] = $nome;
      $_SESSION['sobrenome'] = $sobrenome;
      $_SESSION['email'] = $email;
      $_SESSION['nivel_usuario'] = $nivel_usuario;

      mysql_query(

        "UPDATE xxxxx_usuarios SET data_ultimo_login = now()
        WHERE usuario_id ='{$usuario_id}'"

      );

      header("Location: separador.php");


    }

  }else{

    echo "Você não pode logar-se! Este usuário e/ou senha não são válidos!<br />Por favor tente novamente!<br />";

    include "index.html";

  }

}

?>

this is the separator.php file

<?php

$nivel = $_SESSION['nivel_usuario'];

if ($nivel == 0){
    header("location: /NORMAL/inicio.html");
} else if ($nivel == 1){
    header("location: /PREMIUM/index.html");
} else if ($nivel == 2){
    header("location: /ADM/index.html");
} else {
    header("location: index.html");
} 
?>
    
asked by anonymous 02.08.2018 / 21:03

3 answers

0

You may not need the page separador.php , you can simply use the header to get to the right place, like this:

//Depois de receber o nivel do usuario na session defina a qual pagina será redirecionado
$_SESSION['nivel_usuario'] = $nivel_usuario;

if($nivel_usuario == 1){
    $pagina = "index.php";
}else if($nivel_usuario == 2){
    $pagina = "index2.php";
}else{
    $pagina = "index3.php";
}

//Depois é só concatenar a variavel pagina com o header
header("location:".$pagina.")";

But if you still prefer to redirect to separador.php , you can do the following there:

$tipo = $_SESSION['nivel_usuario'];

if($tipo == 1){
    header("location: index.php");
}else if($tipo == 2){
    header("location: index2.php");
}else{
    header("location: index3.php");
} 
    
02.08.2018 / 21:22
1
  

After the author has published the page separador.php note the absence of session_start()

put session_start (); at the beginning of the separator.php that will work

<?php

session_start();

$nivel = $_SESSION['nivel_usuario'];

if ($nivel == 0){
    header("location: /NORMAL/inicio.html");
} else if ($nivel == 1){
    header("location: /PREMIUM/index.html");
} else if ($nivel == 2){
    header("location: /ADM/index.html");
} else {
    header("location: index.html");
} 
?>
  

If not, session_start(); will fall into the conditional 0 independent if it is in if or any else if

<?php

$nivel = $_SESSION['nivel_usuario'];

if ($nivel == 1){
    header("location: /NORMAL/inicio.html");
} else if ($nivel == 2){
    header("location: /PREMIUM/index.html");
} else if ($nivel == 0){

    /******* Vai redirecionar para esta página *******/
    header("location: /ADM/index.html");
    /************************************************/

} else {
    header("location: index.html");
} 
?>

Why does the conditional == 0 ?

Because if the session is not started $_SESSION['nivel_usuario'] does not exist - check with var_dump ($ _ SESSION ['user_level']) - and $nivel will be NULL , so for PHP NULL == 0 therefore $nivel == 0 is true

  

Another solution would be to allocate the code

$nivel = $_SESSION['nivel_usuario'];

if ($nivel == 0){
    header("location: /NORMAL/inicio.html");
} else if ($nivel == 1){
    header("location: /PREMIUM/index.html");
} else if ($nivel == 2){
    header("location: /ADM/index.html");
} else {
    header("location: index.html");
}
  

inside the verification page instead of header("Location: separador.php");

    
02.08.2018 / 22:09
0

Direct to the page, and in case of a different value, open an error / access page denied:

$nivel = $_SESSION['nivel_usuario'];

if ($nivel == 1){
    header("location: index1.php");
} else if($nivel == 2){
    header("location: index2.php");
} else if($nivel == 3){
    header("location: index3.php");
} else {
    header("location: erro.php");
} 

You can use any other condition structure. Simply filter and direct with header .

    
02.08.2018 / 21:24