Search code examples
phpsessionauthenticationmulti-user

PHP Multi-user Login with Session


I have 7 user levels. I will be redirected depending on my input (for example I input the credentials of the admin, i will be redirected to admin page) and same goes with the other 6. The problem I have is that after successfully logging in, if I change the url from (localhost/admin/home.php) to (localhost/employee/home.php) I can now access the employee's page. I want to have restrictions on that. Or maybe an error that says "Unauthorized user. Access denied." something like that. Here's my code.

index.php

    <form action="checklog.php" method="POST">
     <h1>Log in</h1> 
      <p> 
       <label for="username" class="uname" > Your email or username </label>
       <input id="username" name="username" required="required" type="text" placeholder="myusername " minlength="2" />
      </p>                          
      <p> 
       <label for="password" class="youpasswd"> Your password </label>
       <input id="password" name="password" required="required" type="password" placeholder="eg. X8df!90EO" minlength="2" /> 
      </p>                          
     <input type="submit" name="submit" value="Login">
    </form>

    <?php // To display Error messages
    if(isset($_GET['err'])){
    if ($_GET['err']==1){
    echo "Invalid Credentials.";}
    else if($_GET['err']==5){
    echo "Successfully Logged out";}
    else if ($_GET['err']==2){
    echo "You're trying to access an unauthorized page.";
    }
    }
    ?>
    </body>

checklog.php (This is where I process the credentials.)

    <?php
require_once("db.php");
function check_input($r){
    $r=trim($r);
    $r=strip_tags($r);
    $r=stripslashes($r);
    $r=htmlentities($r);
    $r=mysql_real_escape_string($r);
    return $r;
    }
if (isset($_POST['username'],$_POST['password'])){

    $u=check_input($_POST['username']);
    $p=md5(check_input($_POST['password']));
    try{
    $db=get_db();
    $stmt=$db->prepare("SELECT * FROM users WHERE username=? && password=?");
    $stmt->execute(array($u,$p));
    $r=$stmt->fetch(PDO::FETCH_ASSOC);
    if($r){
        session_start();
        $access_level=$r['access_level'];
        $_SESSION['username']=$r['username'];
        $_SESSION['access_level']=$access_level;
        if ($access_level==0){
            header("Location: admin/home.php");
            }
         if($access_level==1){
            header("Location: user/home.php");
            }
           if($access_level==2){
              header("Location: businesshead/home.php");
              }
            if($access_level==3){
               header("Location: scm/home.php");
               }
             if($access_level==4){
                header("Location: finance/home.php");
                }
              if($access_level==5){
                 header("Location: gm/home.php");
                 }
               if($access_level==6){
                 header("Location: scma/home.php");
               }

        }
    else{
        header("Location:index.php?err=1");
        }
    }
    catch(PDOException $e){
        die("Database error: ".$e->getMessage());
    }
}
else{
    header("Location:index.php");
    }
?>

And lets just assume that this is my admin page (admin.php)

<!DOCTYPE html>
<body>

Welcome!

</body>
</html>

Thank you in advance!


Solution

  • You have to check session on every page. Put related code on top of every page like

    admin page

     <?php   
     session_start();
     if($_SESSION['type'] != 0){
            echo "Unauthorized user. Access denied."
            die; // stop further execution
     } ?>
    

    user page

    <?php   
     session_start();
     if($_SESSION['type'] != 1){
            echo "Unauthorized user. Access denied."
            die; // stop further execution
      } ?>