如何在php中打印剩余的会话时间? [英] How to print session time remaining in php?
问题描述
我有一个 PHP 程序,我想在屏幕上打印用户在他们的会话到期前还有多长时间.我希望它在重新加载页面时更新.
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:
您的会话将在 60 分钟后到期
Your session expires in 60 minutes
...页面重新加载...
...page reload...
您的会话将在 59 分钟后到期
Your session expires in 59 minutes
另外,如何设置超时值?
Also, how do I set the timeout value?
推荐答案
您可以设置一个带有 UNIX 时间的 cookie 并与之进行比较.
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
在此处阅读有关 setcookie 的信息 http://php.net/manual/en/function.setcookie.php
如果您以前从未使用过,请务必仔细阅读开头.
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.
并比较时间,例如:
If (isset($_COOKIE["start"]) and $_COOKIE["start"]+3600<time()){
echo (time()-$_COOKIE["start"])/60 . " minutes left";
}Else{
Echo "times up";
}
这篇关于如何在php中打印剩余的会话时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!