How to check / determine session age using PHP

2019-07-20 15:21发布

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.

2条回答
forever°为你锁心
2楼-- · 2019-07-20 15:31
$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().

查看更多
Bombasti
3楼-- · 2019-07-20 15:34

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

查看更多
登录 后发表回答