嗨我创建session
并希望在1分钟后销毁它,如果用户根据代码不执行任何操作,它应该显示我在一分钟后打印的消息,但它显示什么都不知道为什么?
这是我的代码
session_start();
$_SESSIONlogin_user = 'abc';
$_SESSION['USER']['login_email'] = 'abc@example.com';
$_SESSION['USER']['login_phone'] = '9876543210';
if (isset($_SESSION['USER']['login_email'])) {
$_SESSION['start'] = time();
$_SESSION['expire'] = $_SESSION['start'] + (1 * 60);
$now = time();
if ($now > $_SESSION['expire']) {
session_destroy();
header('Location: dev.ludhianalive.com');
echo "Your session has expired! <a href='#myModal'>Login here</a>";
exit;
}
}
答案 0 :(得分:2)
您的问题是,您在之前>>检查过期时间是否过去时,将未来的到期时间设置为1分钟。因此,到期时间总是在将来。
您应该对支票到期时间以及设定到期时间的代码进行重新排序。
此外,session_destroy()
不会按预期删除用户的数据。在我已包含的documentation for session_destroy中有一个完整的删除会话以记录用户的示例。
session_start();
$_SESSION['login_user'] = 'abc';
$_SESSION['USER']['login_email'] = 'abc@example.com';
$_SESSION['USER']['login_phone'] = '9876543210';
if (isset($_SESSION['USER']['login_email'])) {
$now = time();
if (isset($_SESSION['expire']) && $now > $_SESSION['expire']) {
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Finally, destroy the session.
session_destroy();
header('Location: dev.ludhianalive.com');
echo "Your session has expired! <a href='#myModal'>Login here</a>";
exit;
}
$_SESSION['start'] = $now;
$_SESSION['expire'] = $_SESSION['start'] + (1 * 60);
}