如何在 PHP 中检测用户闲置一段时间并销毁其会话?

发布于 2024-08-19 11:25:34 字数 161 浏览 7 评论 0原文

如果用户闲置 5 分钟,我想让我的 PHP 网站销毁用户会话。如果确实发生这种情况,我想向用户显示一条消息,说明他们注销的原因并将其重定向到登录页面。

处理这个问题的最佳方法是什么?

我在 php 和 myadmin 上运行。

谢谢阿维纳什

I would like to have my PHP website destroy a users session if they have been idle for 5 minutes. If this does happen, I'd like to present the user with a message stating why they were logged out and redirect them to the login page.

What is the best way to handle this?

I am running on php and myadmin.

Thanks

Avinash

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野味少女 2024-08-26 11:25:34

每次加载页面时,都将时间戳存储在会话变量中。当用户转到某个页面时,检查是否 $SESSION['lastActivity'] $SESSION['lastActivity'] $SESSION['lastActivity'] $SESSION['lastActivity'] < time() - <您的空闲时间>。如果为 true,则重定向到“您的会话已过期”页面或类似页面。如果没有,请继续加载页面。

Every time you load a page, store a timestamp in a session variable. When the user goes to a page, check to see if $SESSION['lastActivity'] < time() - <your idle time>. If true, then redirect to a 'your session has expired' page or similar. If not, continue loading the page.

水中月 2024-08-26 11:25:34

我假设你的意思是“空闲”时间。无需计算,但您必须存储该用户最后一次(经过身份验证的)访问并重置计数器。
通常,应该发出具有特定时间戳的身份验证 cookie。验证 cookie 后,您将当前时间与时间戳进行比较。如果此差异大于某个阈值(例如 5 分钟),则会显示错误页面。
为了改善用户体验,您可能还想向用户显示一个计时器,这样就不会出现不好的意外。

I assume you mean 'idle' time. There is no need to calculate that, but you have to store the last (authenticated) access made by that user and reset the counter.
Typically, one would should issue an authentication cookie with a certain timestamp. Upon authentication of the cookie, you compare the current time with the timestamp. If this difference is larger than some threshold, say 5 minutes, you present an error page.
To improve the user experience, you might also want to display a timer to the user so there is no bad surprise.

又怨 2024-08-26 11:25:34

你能不能只使用 PHP 内置的会话函数将会话过期时间设置为 5 分钟。并在会话不再存在时重定向?

Can you not just use PHP's built-in session functions to set session expiry time to 5 mins. and re-direct if a session no longer exists?

一瞬间的火花 2024-08-26 11:25:34

创建一个 JavaScript 函数,该函数具有一分钟的超时时间,可弹出并重定向。

另外,在服务器上确保它们的会话过期,以便在 Javascript 不可用的情况下,它们在空闲时间过后无法继续操作。

Create a javascript function that has a x minute timeout that pops up and redirects.

Also on the server make sure their session expires so that in the case of Javascript not being available, they cannot continue actions after their idle time has passed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文