开发者

php how to execute function on session timeout

开发者 https://www.devze.com 2023-03-14 20:31 出处:网络
I have $_SESSION variables containing user login username that have the default session life set ( 24 minutes I think ).I\'m wondering if there is a way to execute a function on session timeout.

I have $_SESSION variables containing user login username that have the default session life set ( 24 minutes I think ). I'm wondering if there is a way to execute a function on session timeout.

I know I can tell the page to execute the function when the user reloads the page and the session variable has expired but I'm trying to figure out a way for the server to do this by itself. Is there such a method?

My Reasoning: When the user logs in I report to my databa开发者_高级运维se and update the user record for 'lastlogin'. I want to update 'lastlogout' when the user clicks logout or when the $_SESSION variable expires.


Try settings callbacks in session_set_save_handler on close and gc (garbage collector).


You can use javascript for this in about this way:

<?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 timeout period is exhausted. In this script, by this time, the $_SESSION content is already lost (garbage-collected) - and you can make your database calls to update user's record.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号