Use $ _SESSION [] when the user logs in and sets the variable as true. You can also save some data in a session variable. As soon as the user clicks on the exit, the session value should be false. The code may 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 an easy way to do this. If you need a timeout. You can use the start time for the variable $ _SESSION [], and then set the timeout. Check activity before this time, and then exit as follows:
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 may be more effective ways to do this. I am looking for the best way to do it myself. Hope it helps.
So, if he closes his browser, he will still log out after inactivity. But if you want to exit the browser after closing the browser, check ini.session.cookie-lifetime
Maddy source share