Menu by access level

0

Today I was able to mount like this:

On each page I put the code below where I identify the user level

<?php
session_start();
$nivel = 2;
set_time_limit(0);
date_default_timezone_set('America/Sao_Paulo');
include 'adm/config.php';
include 'adm/functions.php';
include 'adm/menu.php';
include("adm/seguro.php");

?>

The seguro.php page that checks the permissions looks like this:

It checks the level that is on the page that the user that accesses, for example if the page is $nivel=1 and the user is level 2 it gives the message: Você não tem o nível de acesso para essa página

<?php

// QUANDO TENTO LOGAR
if(isset($_POST['acesso'])=="Logar") {

// VERIFICANDO SE USUÁRIO E SENHA ESTÃO VAZIOS
if(isset($_POST['usuario'])=="" || isset($_POST['senha'])=="") {
echo "Os dados de acesso devem ser preenchidos";
exit;
}

// LOGANDO E CRIANDO AS SESSIONS
$logar = mysqli_query($conexao,"SELECT usuario, senha, nivel FROM acesso WHERE usuario='".anti_injection($_POST['usuario'])."' AND senha='".anti_injection(md5($_POST['senha']))."' AND nivel='".anti_injection($nivel)."'");
if(mysqli_num_rows($logar) >= 1) {
$_SESSION['usua'] = $_POST['usuario'];
$_SESSION['senh'] = md5($_POST['senha']);
echo "<script>
alert('Acesso permitido');
location.href='index.php';
</script>";
} else {
echo "<script>
alert('Acesso restrito');
</script>";
}

}

// VERIFICANDO SE O NÍVEL DA PÁGINA É VÁLIDA PARA O USUÁRIO LOGADO
if(@$_SESSION['usua'] AND @$_SESSION['senh']) {
$verifica_nivel = mysqli_query($conexao,"SELECT usuario, senha, nivel FROM acesso WHERE usuario='".anti_injection($_SESSION['usua'])."' AND senha='".anti_injection($_SESSION['senh'])."' AND nivel='".anti_injection($nivel)."'");
if(mysqli_num_rows($verifica_nivel) >= 1) {
// ACESSO CORRETO
} else {
echo "<script>
alert('Você não tem o nível de acesso para essa página');
history.back();
</script>";
exit;
}
}

// CASO NÃO LOGADO, MOSTRA O FORMULÁRIO
if(!isset($_SESSION['usua']) OR !isset($_SESSION['senh']) OR $_SESSION['usua']=="" OR $_SESSION['senh']=="") {

?>

<?php
$qr=mysqli_query($conexao,"SELECT DISTINCT usuario FROM 'acesso' ORDER BY 'acesso'.'usuario' ASC");
if (mysqli_num_rows($qr)==0)
    echo "Adicione ao menos um Usuário";

else{
          }  ?>

That way it works, I just wanted to get that level in BD and display different menus. And I can not leave on each page the level of access to prevent it from being accessed by the direct link of the page.

Example:

If it is level 1 it shows include 'adm/menu1.php' ;

If it is level 2 it shows include 'adm/menu2.php' ;

And there is a problem, level 1 is adm . In addition to the pages that have access, it can also access the level 2 pages.

Can anyone give me a light how can this be done?

    
asked by anonymous 19.04.2018 / 15:26

0 answers