I'd use PHP's authentication features. You just send the proper headers if you're not logged in, and it gives you the standard login box. Like this:
<?
function authUser() {
global $PHP_AUTH_USER, $PHP_AUTH_PW
if(!isset($PHP_AUTH_USER)) {
Header("WWW-Authenticate: Basic realm=\"Web Accounting\"");
Header("HTTP/1.0 401 Unauthorized");
echo "You are not authorized for this security level!\n";
return false;
}
elseif ($PHP_AUTH_USER && $PHP_AUTH_PW) {
//do some authentication against the database
}
}
?>
Use a database that stores usernames, passwords, and a timestamp, and have the function return false if the timestamp is out of range.
To protect pages, just put this in the page you want to protect:
if(!$auth_user())
exit();