简体   繁体   English

会话结束后如何在PHP中自动设置注销

[英]How to set logout automatically in PHP after session ends

Please do not mark this as duplicate just by looking at the title.请不要仅通过查看标题将其标记为重复。 I've seen some post related to this.我看过一些与此相关的帖子。 But couldn't find one matching my query.但找不到匹配我的查询。

I am creating a web-site with user login.我正在创建一个用户登录的网站。 On the index page I've set在我设置的索引页上

session_set_cookie_params(7200);

so that the session will be active for 2 hours even if browser is closed.这样即使浏览器关闭,会话也将处于活动状态 2 小时。 But I want the user to be logged out after 2 hours.但我希望用户在 2 小时后注销。

I have created the website so that user can login anytime and it will stay for 2 hours.我已经创建了网站,以便用户可以随时登录,并且会停留 2 小时。 But how to modify it so that page is available for two hours fixed, say (9-11 am) regardless of when the user logs in the time between?但是如何修改它以便页面在固定的两个小时内可用,例如(上午 9 点到 11 点),而不管用户何时登录?

Save in your database the login time of the user.将用户的登录时间保存在您的数据库中。

Compare then the current time with the login time and see if it's more than two hours.然后将当前时间与登录时间进行比较,看看是否超过两个小时。

If so, perform the logout.如果是,请执行注销。


Pseudocode:伪代码:

$currentDate = date('U');
$loginDate   = $dateComingFromTheDatabase; // UNIX timestamp

if (($currentDate - $loginDate) >= 7200)
{
    PerformLogout();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM