我正在构建一个在线订票网站。在这方面我正在做以下事情: 用户用他们的座位号搜索公共(public)汽车。使用 temp_seat_book = 'Y'
使用座位号更新数据库。如果他预订了付费机票,他的状态将更新为 final_ticket_book = 'Y'
。现在我想删除 temp_seat_book = 'Y'
的字段但是
final_ticket_book = 'N'
。为此,我需要删除超过 10 分钟的 session_ids 和 final_ticket_book = 'N'
。那么如何实现后台作业呢?
最佳答案
而不是搜索文件(这涉及更多的 i/o )等,
什么是 session cookie:Session Cookie
更好的方法是在 $_SESSION 变量中存储“最近事件”的时间戳。
并根据每个请求更新 session 数据(包括自动定期 ajax 调用,如果有的话)。
假设您想在 10 分钟后取消设置,
if (isset($_SESSION['most_recent_activity']) &&
(time() - $_SESSION['most_recent_activity'] > 600)) {
//600 seconds = 10 minutes
session_destroy();
session_unset();
}
$_SESSION['most_recent_activity'] = time(); // the start of the session.
To avoid attacks like Session fixation: (Session Fixation is an attack that permits an attacker to hijack a valid user session) keep regenerating the session id periodically say for 5 mins (I would suggest to keep the regeneration time as well as session expire time a bit more). A more elaborate list of attacks: attack list.
if (!isset($_SESSION['CREATED'])) {
$_SESSION['CREATED'] = time();
}
else if (time() - $_SESSION['CREATED'] > 600) {
session_regenerate_id(true);
$_SESSION['CREATED'] = time();
}
此外,请确保将 session.gc-maxlifetime
设置为您要使用的最长过期时间。
你可以这样做
ini_set('session.gc-maxlifetime', 600)
或者
直接在您的 php.ini 中设置它。
还有
session.cookie_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser.
But, destroying the session must be taken care at the server-side and not the client-side. Setting the session.cookie_lifetime set to 0 would make the session’s cookie behave the way a session cookie should i.e. that a session cookie is only valid until the browser is closed.
虽然这种方法有点繁琐,但是比较优雅。
啊,找到我很久以前看过的链接了! : How do I expire a PHP session after 30 minutes?
关于php - 一段时间后取消设置 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8633374/