How to redirect user after login according to user level?

Asked

Viewed 183 times

0

in the system there are 2 user levels 1 = admin and 2 = helper, and after logging in need redirect each to a different page. This is my php code

<?php

session_start();
include('conexao.php');

if (empty($_POST['usuario']) || empty($_POST['senha'])) {
    header('Location: index.php');
    exit();
}
$senha = $usuario = $user_tipo = "";

$usuario = mysqli_real_escape_string($conexao, $_POST['usuario']);
$senha = mysqli_real_escape_string($conexao, $_POST['senha']);
$nivel = mysqli_real_escape_string($conexao, $_POST['nivel']);


$query = "select nome, usuario, nivel from usuario where usuario = '{$usuario}' and senha = md5('{$senha}')";

$result = mysqli_query($conexao, $query);

$row = mysqli_num_rows($result);

$redirecionamentos = [
    '1' => 'usuarios.php',
    '2' => 'downloads.php',
];
if (array_key_exists($_SESSION['nivel'], $redirecionamentos)) {
    header('Location: ' . $redirecionamentos[$_SESSION['nivel']], true, 302);
}
if ($row == 1) {
    $_SESSION['usuario'] = $usuario;
    header($redirecionamentos);
    exit();
} else {
    $_SESSION['nao_autenticado'] = true;
    header('Location: index.php');
    exit();
}

And this error returns to me when I access

inserir a descrição da imagem aqui

  • Not working? What was the error message? What are the latest messages in the server log file?

  • Well it is not authenticating nor redirecting the type of user to the specific page,no log appears these errors[26-Dec-2018 13:56:18 UTC] PHP Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: NO) in /home1/xopen037/public_html/Gob.logosassessoriaconsultoria.com.br/gobdowloads/conexao.php on line 8 [27-Dec-2018 12:41:25 UTC] PHP Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (password using: NO) in /home1/xopen037/public_html/Gob.logosassessoriaconsultoria.com.br/gobdowloads/conexao.php on line 8

  • That is, the database connection failed. Check the connection data and make sure the database server is connected.

  • i am doing the editing on wampserver local server, not on the online server

2 answers

0

after logging in instead of creating a direct redirect link, it does a validation for redirects of each user level, i.e.:

$query = "select nome, usuario, nivel from usuario where usuario = '{$usuario}' and senha = md5('{$senha}')";
$result = mysqli_query($conexao, $query);
$row = mysqli_num_rows($result);
$linha = mysqli_fetch_assoc($result);

validates

if($row > 0) {
  if($linha['nivel'] == 1) {
    header("Location: redireciona-nivel-1.php"); 
  }
else if($linha[nivel] == 2) {
header("Location: redireciona-nivel-2.php"); // e assim sucessivamente...
}
}
  • Thanks Jefferson for the help, but the redirected pages must have what function code? because I am using this // Session checker session_start(); include('verifica_login.php'); ? > Only it is not authenticating, but when I put a page of type redirect-level-1.php (unknown) ends up redirecting

  • and the verification code.php is <?php if(!isset($_SESSION)){ session_start(); } if(!$_SESSION['user']) { header('Location: index.php'); Exit(); }

  • On the restructuring page you validate for each level for example: if( isset($_Session 'user ) Empty($_Session 'user' )) { //Will check if the session exists //then apply here the level validation if($line_sessao['nivel'] == 1) { Content of level 1 } Else { redirects to the login or the content q you want } ; }

0

I’ve adjusted the code now

<?php

session_start();
include('conexao.php');

if (empty($_POST['usuario']) || empty($_POST['senha'])) {
    header('Location: index.php');
    exit();
}
$senha = $usuario = $user_tipo = "";

$usuario = mysqli_real_escape_string($conexao, $_POST['usuario']);
$senha = mysqli_real_escape_string($conexao, $_POST['senha']);
$nivel = mysqli_real_escape_string($conexao, $_POST['nivel']);


$query = "select nome, usuario, nivel from usuario where usuario = '{$usuario}' and senha = md5('{$senha}')";
$result = mysqli_query($conexao, $query);
$row = mysqli_num_rows($result);
$linha = mysqli_fetch_assoc($result);

$redirecionamentos = [
    '1' => 'usuarios.php',
    '2' => 'downloads.php',
];
if (array_key_exists($_SESSION['nivel'], $redirecionamentos)) {
    header('Location: ' . $redirecionamentos[$_SESSION['nivel']], true, 302);
}
if ($row > 0) {
    if ($linha['nivel'] == 1) {
        header("Location: usuarios.php");
    } else if ($linha['nivel'] == 2) {
        header("Location: legislativo.php");
    } 
    else {
        $_SESSION['nao_autenticado'] = true;
        header('Location: index.php');
        exit();
    }
}

Browser other questions tagged

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