2

In my login system code i have 3 users.

But other users can access my admin page.

I want that my admin page can be accessible only for users that has the user level =1 that is the admin.

my code.

<?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";

   }
   }
   ?>

why other two users can access my admin page? whats wrong?

James Allan
  • 192
  • 4
  • 16
  • Do you check on your admin page if the user has the right to access the admin page? – Charlotte Dunois Jul 26 '16 at 18:33
  • 2
    You're not even escaping the form input! – Vahid Amiri Jul 26 '16 at 18:34
  • so, what's inside painel.php ? and what type is the userLevel column? either way, you've been given answers below, so you'll need to ask them – Funk Forty Niner Jul 26 '16 at 18:39
  • 3
    Passwords should be hashed. You should use parameterized queries to prevent SQL injections. – chris85 Jul 26 '16 at 18:39
  • 1
    no comment; ok. well I'm out. – Funk Forty Niner Jul 26 '16 at 18:46
  • I'm learning php and all this is my training – James Allan Jul 26 '16 at 18:55
  • btw thx for the tips guys – James Allan Jul 26 '16 at 19:04
  • 1
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! [Don't believe it?](http://stackoverflow.com/q/38297105/1011527) – Jay Blanchard Jul 26 '16 at 19:51
  • 1
    **Never store plain text passwords!** Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). Make sure you ***[don't escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Jul 26 '16 at 19:51
  • 2
    Never store actual user information in session variables. – Jay Blanchard Jul 26 '16 at 19:52

3 Answers3

2

you have to put the same checking code starting of each page. what i mean is in the admin_panel.php, place like this.

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

I think your problem is if and else statements.In your code if userLevel is NOT 1 it headers it to cliente.php without looking at code after that.

  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";
   }
COp
  • 35
  • 12
1

header does not stop the execution of the script, you have to add exit right after:

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

Find all your headers and add exit after them.