<?php
session_start();
define ("TIMEOUT", 10); //set this to the number of minutes
//use this script by just including the page at the top of every real page
if (!loggedon()):
login();
else:
//do nothing
endif;
function loggedon() {
//this tests the current status
if (isset ($_SESSION['loggedon'])):
if (login_expired()):
$GLOBALS['msg'] = "Login expired";
return false;
else:
$_SESSION['lastaccess'] = strtotime("now");
return true;
endif;
else:
$GLOBALS['msg'] = "You must log on to access this page";
return false;
endif;
}
function login_expired() {
if (isset($_SESSION['lastaccess'])):
if ( ($_SESSION['lastaccess'] + (TIMEOUT * 60 * 60) ) < strtotime("now") ):
return true;
else:
return false;
endif;
else:
return true;
endif;
}
function logout($msg=NULL)
{
unset ($_SESSION['loggedon']);
/* if (isset($_COOKIE[session_name()])):
setcookie(session_name(), '', time()-42000, '/');
endif; */
if (!empty($msg)) $GLOBALS['msg'] = $msg;
display_login();
}
//master script
function login()
{
if (!isset ($_POST['submit'])):
logout();
endif;
switch ($_POST['submit']):
case "Go":
if (!test_fresh_login()):
logout("You cannot use the back button to login");
endif;
if (!validlogon()):
logout("Either username or password is incorrect");
else:
$_SESSION['username'] = $_POST['username'];
$_SESSION['loggedon'] = true;
$_SESSION['lastaccess'] = strtotime("now");
unset ($_POST);
endif;
break;
default:
logout();
endswitch;
}
function test_fresh_login()
{
if (isset($_SESSION['uniqid'])):
if (isset($_POST['uniqid'])):
if ($_SESSION['uniqid'] === $_POST['uniqid']):
unset ($_SESSION['uniqid']);
return true;
else:
return false;
endif;
else:
return false;
endif;
else:
return false;
endif;
}
function validlogon()
{
print_r($_POST);
if (!isset ($_POST['username']) || !isset ($_POST['pwd'])):
return false;
endif;
$valids = array ("admin"=>"password"); //include list of valid username/passwords here
if (
(isset($valids[$_POST['username']]))
&&
($valids[$_POST['username']] == $_POST['pwd'])):
return true;
else:
return false;
endif;
}
function display_login() {
echo "<hr>";
print_r($_SESSION);
print_r($_POST);
echo "<hr>";
$_SESSION['uniqid'] = uniqid("el_can_");
?>
<style type="text/css">
#loginform, {text-align:left;width:50%;border: 1px solid #669966;font-size:14px; margin:0 auto; font-family:Verdana, Arial, Helvetica, sans-serif;}
#loginform .row {clear:both;}
#loginform .field{float:right; width:57%; padding-left:1px; text-align:left}
#loginform .label {float:left; width:39%; padding-right:1px; padding-left:1px;; text-align:right;}
#loginform .row input[type="text"] {width: 90%;}
#loginform .spacer {line-height:1px;}
#loginform input {font-size:14px;}
#loginform .loginmessage {clear:both; width:100%; color:red; text-align:center;}
</style>
<div id="loginform">
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="hidden" name="uniqid" value="<?=$_SESSION['uniqid'] ?>" />
<input type="hidden" name="action" value="login" />
<div class="spacer"> </div>
<div class="row">
<span class="label">Username:</span>
<span class="field"><input type="text" name="username" /></span>
</div>
<div class="row">
<span class="label">Password:</span>
<span class="field"><input type="text" name="pwd" />
<input type="submit" name="submit" value="Go" /></span>
</div>
</form>
<? if (!empty($GLOBALS['msg'])) echo "<span class=\"loginmessage\">{$GLOBALS['msg']}</span>"; ?>
</div> <!-- end login form -->
<?
exit;
}
?>