How to print session time remaining in php?

牧云@^-^@ 提交于 2019-12-01 11:35:10

问题


I have a program in PHP and I want to print to the screen how much time the user has until their session expires. I want it to update when they reload the page.

For example a simple message like this:

Your session expires in 60 minutes

...page reload...

Your session expires in 59 minutes

Also, how do I set the timeout value?


回答1:


You can set a cookie with the UNIX time and compare with that.

setcookie("start", time(), time ()+3600); // saves a cookie with current UNIX time for one hour

Read about setcookie here http://php.net/manual/en/function.setcookie.php
If you never used it before, be sure to read the begining carefully.

And compare time like:

If (isset($_COOKIE["start"]) and $_COOKIE["start"]+3600<time()){
    echo (time()-$_COOKIE["start"])/60 . " minutes left";
}Else{
    Echo "times up";
}


来源:https://stackoverflow.com/questions/44759153/how-to-print-session-time-remaining-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!