Php how to execute a function in session timeout

I have $_SESSION variables containing the login name of the user who has the default session (it seems like 24 minutes). I am wondering if there is a way to execute a function in session timeout.

I know that I can say that the page performs this function when the user reloads the page and the session variable has expired, but I'm trying to figure out how the server can do this itself. Is there such a method?

My Reasoning When a user logs in, I submit to my database and update the user record for "lastlogin". I want to update "lastlogout" when the user clicks the exit button or when the $_SESSION variable expires.

+4
source share
2 answers

Try calling back into session_set_save_handler in close and gc (garbage collector).

+5
source

You can use javascript to do this like this:

 <?php ... $currentTimeoutInMillis = ini_get('session.gc_maxlifetime') * 1000; print "<script type='text/javascript'>\n"; print "<!--\n"; print "setTimeout('window.location.href=\"timeout.php\"', $currentTimeoutInMillis);\n"; print "//-->\n"; ... ?> 

Then your timeout.php will be called automatically when the wait period has expired. In this script, by this time, the contents of $ _SESSION have already been lost (garbage collected) - and you can make your database calls to update the user record.

0
source

All Articles