targeting users validated by php

Asked

Viewed 119 times

0

good afternoon i made a system, bd ok and loguin check ok however wanted after checking the user it open the right page for the user certain example: I have 3 levels being 0 1 2 and I want the user level 0 open the page index1.php the level user 1 open the page index2.php and the level user 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";

  }

}

?>

where when it executes the header("Location: separator.php") directs the open and validated user to its right page tried commands with if Else switch but Nunk get some idea.

this is the.php separator 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");
} 
?>
  • How do you know the user level? You are in the bank?

  • Yes this in the bank discriminated

  • I was able to resolve relocating the code inside the verification page as proposed by @João Victor Souza... Personal upgrade

  • Now that we’ve seen your.php tab, it’s clear that session_start() is missing; at the beginning of the page

3 answers

1

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

put session_start(); at the beginning of the.php tab 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 put session_start(); will fall on parole 0 whether it is in the 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");
} 
?>

'Cause I get paroled == 0?

Because if the session is not started $_SESSION['nivel_usuario'] there is no - check with var_dump($_SESSION['nivel_usuario']) - and $nivel will be NULL, therefore, for PHP NULL == 0 consequently $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");
}

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

  • @Anderson Carlos Woss and Guilherme Nascimento, now the answer was supimpa!

0


You may not need the page separador.php, you can simply use the header to take you 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 the page separador.php, 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");
} 
  • sorry my ignorance but this last one would not direct direct to the index3

  • He would only redirect to index3.php if the type was not equal to 1 or equal to 2

  • You can also add one last if to check if it is equal to 3, but I do not believe that it is necessary, would thus be the last: }else if($tipo == 3){ header("location: index3.php"); }

  • I’m using if chained or Else if, if you do not know can read more about in the php documentation : Structure Else if

  • good day this option inside the tab only forwards to type 2 even though I am type 1 kkkk tava perdidao even with little knowledge in php

  • Good morning @Eduardosaraiva, try quoting the comparison to see if it goes, like this: ($tipo == "1"), do with everyone

  • put and played straight into the error

  • You’re wearing session_start(); on the page php separator. ?? if not, try this

Show 3 more comments

0

Direct to the page, and if any different value opens an error/access denied page:

$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 the header.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.