Hi all,
I’m trying to get some code to prevent users who do not have the rights to the admin area from accessing it.
This is my session being set at login;
[php]
//start session
$_SESSION[‘firstName’] = $firstName;
$_SESSION[‘surname’] = $surname;
$_SESSION[‘username’] = $username;
$_SESSION[‘login’] = true;
$_SESSION[‘admin’] = $admin;
header(“Location: account.php”);
} else {
//redirect back to login form if not authorised
echo ‘Invalid username or password. You will be automatically redirected to login.’;
header(“refresh:5; url=login.php”);
exit;
[/php]
This is the admin page code;
[php]
session_start();
// if username is set, allow into admin area
if(!isset($_SESSION[‘admin’]) || !in_array($_SESSION[‘admin’], array(‘1’)))
{
//session is set, user is logged in
header(“Location: login.php”);
}
[/php]
I also tried;
[php]
// if admin is set, allow into admin area
if(isset($_SESSION[‘admin’])){
//session is set, user is logged in
}else{
// send to login page if not logged in
header(“Location: login.php”);
}
[/php]
If they are an admin user, I have an admin field in my database to store a 1 for admin and 0 for non-admin.
When I logged in with an admin account, I echoed the value and it does echo a 1.
Thanks,
Jack