我运行的脚本会在一定的非活动时间后销毁用户的会话。但是,它运行不正常。有人可以向我解释我做错了吗?
<?php
require("../includes/header.php");
$expire = time();
echo $expire ."<br>";
if(!isset($_SESSION["expire"]) < ($expire + 30)){
setcookie("User", $_SESSION["user"], 30);
echo "Welcome " .$_SESSION["user"];
$_SESSION["expire"] = $expire;
}
elseif($_SESSION["expire"] > ($expire + 30)){
unset($_COOKIE["User"]);
session_unset();
session_destroy();
header("Location: logged_out.php");
}
?>
答案 0 :(得分:3)
$expire
始终等于$_SESSION["expire"]
,因为您在页面顶部设置$_SESSION["expire"]
等于$expire
,并且永远不会更改其值。
在验证用户之后设置$_SESSION["expire"]
。此外,您的逻辑似乎不正确:
<?php
require("../includes/header.php");
$now = time();
$expires = $_SESSION["expire"] + 30;
if(!isset($_SESSION["expire"]) || $expires > $now){
setcookie("User", $_SESSION["user"], 30);
echo "Welcome " .$_SESSION["user"];
$_SESSION["expire"] = $now;
}
else {
unset($_COOKIE["User"]);
session_unset();
session_destroy();
header("Location: logged_out.php");
}
?>