Php 3级用户登录系统

Php 3级用户登录系统,php,mysqli,Php,Mysqli,在我的登录系统代码中,我有3个用户 但是其他用户可以访问我的管理页面 我希望我的管理页面只能对用户级别为1的用户访问,即管理员 我的密码 <?php session_start(); include 'includes/dbh.php'; //You can add more params here for more register options $email= $_POST['email']; $password= $_POST['password']; $sql =("SELE

在我的登录系统代码中,我有3个用户

但是其他用户可以访问我的管理页面

我希望我的管理页面只能对用户级别为1的用户访问,即管理员

我的密码

<?php
session_start();
include 'includes/dbh.php';
//You can add more params here for more register options
$email= $_POST['email'];
$password= $_POST['password'];


$sql =("SELECT * FROM usuarios WHERE email='$email' 
AND password='$password'");
$result = $conn->query($sql);
if(!$row = $result->fetch_assoc()) {
$_SESSION['errorLogin']="Usuario ou senha inválida";
echo "".$_SESSION['errorLogin'];
header("Location: login.php");

}else{


$_SESSION['userName']=          $row ['nome'];
$_SESSION['userLName']=         $row ['sobrenome'];
$_SESSION['userLevel']=         $row ['userLevel'];
$_SESSION['useremail']=         $row ['email'];
$_SESSION['password']=          $row ['password'];
$_SESSION['userContact']=       $row ['contato'];
$_SESSION['userContact2']=      $row ['contato2'];
$_SESSION['role']=              $row ['login'];
  if($_SESSION['userLevel'] == 1){
   header("Location: adm/painel.php");
   }else{ header("Location: cliente.php"); 

   }
   if($_SESSION['userLevel'] == 2){
   header("Location: rep_page.php");
   }else{ header ("Location: rep_page.php");}

   if($_SESSION['userLevel'] == 3){
   header("Location: cliente.php");

   }else{
   echo "Your not logged in";

   }
   }
   ?>
为什么其他两个用户可以访问我的管理页面?
有什么问题吗?

我认为您的问题在于if和else语句。在您的代码中,如果userLevel不是1,它会将其头文件发送到cliente.php,而不查看后面的代码

  if($_SESSION['userLevel'] == 1){
   header("Location: adm/painel.php");
   }elseif($_SESSION['userLevel'] == 2){
   header("Location: rep_page.php");
   }elseif($_SESSION['userLevel'] == 3){
   header("Location: cliente.php");
   }else{
   echo "Your not logged in";
   }
标头不会停止脚本的执行,您必须在以下内容之后立即添加退出:

if($_SESSION['userLevel'] == 1){
   header("Location: adm/painel.php");
   exit;                                     //◄■■■■■■■■■■■■■■■■■■■■■
   }else{ header("Location: cliente.php"); 
          exit;                              //◄■■■■■■■■■■■■■■■■■■■■■
   }

找到所有标题并在其后面添加“退出”。

您必须在每页的开头添加相同的检查代码。我的意思是 在admin_panel.php中,像这样放置

if($_SESSION['userLevel'] == 1){
 //here please place the entire page code
}else{ 
 echo "you have no access this page";
}

你甚至都不能逃避表单输入!密码应该是散列的。您应该使用参数化查询来防止SQL注入。无注释;好啊我出去了。说。即使是这样也不安全!永远不要在会话变量中存储实际的用户信息。答案应该有一个解释。在那之后,我可以打印消息并将用户发送到登录页面吗?yup headerLocation:logout.php;就像我想打印消息,几秒钟后再发送给用户登录。做一件事,创建一个这样的错误会话$_会话['error']=很抱歉,您的页面不正确;`在登录页面中,首先检查错误会话,ifisset$_会话['error']{echo$_会话['err']];}