Use a $_SESSION[] when a user logs in and set the variable to be true. You can save some data in the session variable too if you need. Once user clicks on logout set the session variable to be false. The code could be as follows:
function Login()
{
if(!isset($_SESSION)){ session_start(); }
if(!$this->CheckLoginInDB($email,$password))
{
return false;
}
$_SESSION[$this->GetLoginSessionVar()] = $email;
return true;
}
function GetLoginSessionVar()
{
$retvar = md5($this->rand_key);
$retvar = 'usr_'.substr($retvar,0,10);
return $retvar;
}
function LogOut()
{
session_start();
$sessionvar = $this->GetLoginSessionVar();
$_SESSION[$sessionvar]=NULL;
unset($_SESSION[$sessionvar]);
}
This is one simple way of doing it. If you want a time out. You can use a start time for your $_SESSION[] variable and then set a time out time. Check for any activity till that time and then sign out like this:
function LogOut(){
session_start();
// set timeout period. This will be in seconds.
$inactive = 1000;
// check to see if $_SESSION['timeout'] is set
if(isset($_SESSION['timeout']) ) {
$session_life = time() - $_SESSION['start'];
if($session_life > $inactive)
{ session_destroy(); header("Location: logoutpage.php"); }
}
$_SESSION['timeout'] = time();
}
There could be more better ways of doing this. I am looking for a better way to do it myself . Hope it helps.
So if he closes his browser he will be logged out after inactivity anyway. But if you want logout after browser is closed check ini.session.cookie-lifetime