-1
<?php
session_start();
if (empty(isset($_SESSION['user']))) {
    header('location:index.php');
}
if (isset($_SESSION['role']) == 'admin') {
    header('location:admin.php');
}
if (isset($_SESSION['role']) == 'agent') {
    header('location:user.php');
}
if (isset($_SESSION['role']) == 'supervisor') {
    header('location:supervisor.php');
}

?>
<html>
<head>
<title>PHP MySQL Role Based Access Control</title>
</head>
<body>
    <div align="center">
        <h3>PHP MySQL Role Based Access Control</h3>
        <form method="POST" action="includes/login.php">
            <table>
                <tr>
                    <td>UserName:</td>
                    <td><input type="text" name="username" /></td>
                </tr>
                <tr>
                    <td>PassWord:</td>
                    <td><input type="text" name="password" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" name="login" value="Login" /></td>
                </tr>
            </table>
        </form>
<?php if(isset($error)){ echo $error; }?>
</div>
</body>
</html>

// trying to add some sessions for various login users if they accidently closed the browser window before they log out it should redirect them to the reffered pages not to index pages

Erubiel
  • 2,934
  • 14
  • 32
ram
  • 1
  • 1
  • 1
    You need to change `isset($_SESSION['role']) == 'admin'` to `isset($_SESSION['role']) && $_SESSION['role'] == 'admin'` (and the same for the other two tests) – Nick Aug 30 '18 at 02:33
  • thanks for the help guys – ram Aug 30 '18 at 18:19

1 Answers1

0

try this. i hope it will help you.

session_start();
 if (isset($_SESSION['user']['user_id'])) {
            if ($_SESSION['user']['role'] == 'admin') {
                header('location:admin.php');

            } else if ($_SESSION['user']['role'] == 'agent') {
               header('location:user.php');

            } else if ($_SESSION['user']['role'] == 'supervisor') {
               header('location:supervisor.php');
            } 
PHP Geek
  • 3,949
  • 1
  • 16
  • 32
  • Should probably throw an `exit` in there after each call to `header`. See https://stackoverflow.com/questions/2747791/why-i-have-to-call-exit-after-redirection-through-headerlocation-in-php –  Aug 30 '18 at 20:42
  • if this answer is helpful for you then please upvote. – PHP Geek Aug 31 '18 at 04:13