In the head of each page add this
Note this must be before any html header are sent (i.e. in php code before html output)
<?php
//----- CHECK USER IS LOGGED IN -----
session_start();
if ( (!isset($_SESSION['userisloggedin'])) || ($_SESSION['userisloggedin'] != "YES") )
{
header("Location: login.php");
die();
}
?>
Use this as the log in page
<?php
//------------------------------------
//----- CHECK FOR FORM SUBMITTED -----
//------------------------------------
session_start();
if ( (isset($_POST['action'])) && ($_POST['action'] == "log_user_in") )
{
if (trim($_POST['Password']) == "1234")
{
$_SESSION['userisloggedin'] = "YES";
header("Location: index.php");
die();
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Log In</title>
<style type="text/css">
<!--
body {
font-family: Arial, Helvetica, sans-serif;
font-size: small;
color: #000033;
}
-->
</style>
</head>
<body>
<p>Log In</p>
<?php
echo <<<END
<form id="MyForm" action="login.php" method="POST">
<input type="hidden" name="action" value="log_user_in" />
<p>Password:</p>
<input type="text" name="Password" size="24" autofocus ></input>
<input type="submit" value="Log In" id="Send" />
</form>
END;
?>
</body>
</html>
Use this as an optional log out page
<?php
session_start();
$_SESSION['userisloggedin'] = "NO";
header("Location: index.php");
die();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Log Out</title>
</head>
<body>
</body>
</html>
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.