I use the below code on all my pages after initialising the SESSION data and defining the variable $auth_level.
I use this to decide what to show users of varying levels.
<?php
if($auth_level == 'basic'){
// auth_level basic
if (!isset($_SESSION['username'])) {
header('Location: login.php');
}
} else if ($auth_level == 'admin'){
// auth level admin
if (!isset($_SESSION['username']) || $_SESSION['role'] != 2) {
header('Location: login.php');
}
} else {
// auth level admin assumed for security
if (!isset($_SESSION['username']) || $_SESSION['role'] != 2) {
header('Location: login.php');
}
}
?>