This topic is locked
[SOLVED]

 Session Timeout

9/23/2020 5:16:37 PM
PHPRunner General questions
B
bob1234 author

Hi Forum
Does anyone know if there is a way to setup a User Session Timeout function / feature in phpr
Thanks

Robert

D
david22585 9/25/2020



Hi Forum
Does anyone know if there is a way to setup a User Session Timeout function / feature in phpr
Thanks

Robert


Could be easy, depending on what you're looking for. Do you want their login to expire after a certain amount of time/days? Or do you want something that after a certain amount of inactivity, it logs them out?

B
bob1234 author 9/26/2020



Could be easy, depending on what you're looking for. Do you want their login to expire after a certain amount of time/days? Or do you want something that after a certain amount of inactivity, it logs them out?


Hi David
thanks for the reply, I am looking for something that after a certain amount of inactivity, it logs them out.
Thanks

Robert

D
david22585 9/26/2020



Hi David
thanks for the reply, I am looking for something that after a certain amount of inactivity, it logs them out.
Thanks

Robert


I'll throw together something for you this evening when I'm home.

D
david22585 9/27/2020

This is just a quick idea to get you on the right path, but in events -> after app init:
Then you need to compare the time you want them to logoff with to the current time:



$_SESSION["msg"] = "";

if($_SESSION["UserID"]){

$minutes=20;

$t=date('Y-m-d H:i:s', time()-$minutes*60);

$rs = DB::Query("SELECT activity FROM users WHERE id=".$_SESSION["UserID"]."");

$data = $rs->fetchAssoc();

if($data["activity"] > '".$t."'){

Security::logout();

header("Location: login.php");

$message = "For security, your account has been logged out.";

exit();

}

}


After this code, you need to add a new update so that if they are within the window of activity, it updates their time:



if($_SESSION["UserID"]){

DB::Query("UPDATE activity FROM users WHERE id=".$_SESSION["UserID"]."");

}


Now you need to put this into 2 spots, the after app init, abd after successful login. This way once they login, it will set a new time that the 1st code will read. Some inspiration was taken from here: https://xlinesoft.com/blog/2015/04/21/displaying-a-list-of-users-that-are-currently-logged-in/

B
bob1234 author 9/27/2020



This is just a quick idea to get you on the right path, but in events -> after app init:
Then you need to compare the time you want them to logoff with to the current time:



$_SESSION["msg"] = "";

if($_SESSION["UserID"]){

$minutes=20;

$t=date('Y-m-d H:i:s', time()-$minutes*60);

$rs = DB::Query("SELECT activity FROM users WHERE id=".$_SESSION["UserID"]."");

$data = $rs->fetchAssoc();

if($data["activity"] > '".$t."'){

Security::logout();

header("Location: login.php");

$message = "For security, your account has been logged out.";

exit();

}

}


After this code, you need to add a new update so that if they are within the window of activity, it updates their time:



if($_SESSION["UserID"]){

DB::Query("UPDATE activity FROM users WHERE id=".$_SESSION["UserID"]."");

}


Now you need to put this into 2 spots, the after app init, abd after successful login. This way once they login, it will set a new time that the 1st code will read. Some inspiration was taken from here: https://xlinesoft.com/blog/2015/04/21/displaying-a-list-of-users-that-are-currently-logged-in/


Hi David
Thanks so much for this, I will give it a try tomorrow, sorry for the late reply been a busy weekend
Thanks

Robert