How to check / determine session age using PHP

2019-07-20 15:30发布

问题:

I wonder if it's possible to check session age somehow with PHP. I have browsed trough stackoverflow and have not found anything. Thanks for all answers in advance.

In case anyone wonder why would I need to know something like this is, I want to show notification on top of the screen on all pages for all people are logged in less then 30 minutes.

回答1:

Why not just set $_SESSION['logged_in_at'] = time() when the user logs in, then check if( time() - $_SESSION['logged_in_at'] < 30*60)?



回答2:

$session_time = fileatime(session_save_path()."/sess_".session_id());
echo "Session Age (Seconds): ".(time()-$session_time);

Perhaps it isn't best practice but it'll do, in the absence of a native function like session_time().