What I do is when the user logs in, I set a session variable, like so
$_SESSION['loggedin'] = true;
Then, on each page I do this at the VERY beginning of the page
session_start();
if(!isset($_SESSION['loggedin'] || $_SESSION['loggedin'] !== true){
header('Location: http://www.foo.com/login.php');
exit(); <-- THIS IS VERY IMPORTANT
}
MAKE SURE to include the exit(); function call. Otherwise, the client could deny the redirect request, and the rest of the page would render. This way, even if the client denies the redirect request, all they get is a blank page. have fun :)
PS: If you want to be clever and prevent yourself from repeating code, create a PHP file called "functions.php" (or something simmilar that you like). Then, inside of functions.php create a function called "requireLogin()"
function requireLogin(){
if(!isset($_SESSION['loggedin'] || $_SESSION['loggedin'] !== true){
header('Location: http://www.foo.com/login.php');
exit(); <-- THIS IS VERY IMPORTANT
}
}
Now, in each page that you want to protect, do this
session_start();
require('functions.php');
reuireLogin();
BOOM page is protected, and the beginning of your pages look much cleaner.