我遇到一个涉及 PHP session 的文件的问题。
我有以下两个 php 文件:
confirm_save.php
<?php
include("confirm.php");
$_SESSION = array();
$token = $_SESSION['logout'];
if ($token && $_POST['token']==$token) {
if(isset($_COOKIE["id"]) && isset($_COOKIE["user"]) && isset($_COOKIE["pass"])) {
setcookie("id", '', strtotime( '-5 days' ), '/');
setcookie("user", '', strtotime( '-5 days' ), '/');
setcookie("pass", '', strtotime( '-5 days' ), '/');
}
// Destroy the session variables
session_destroy();
/*// Double check to see if their sessions exists
if(isset($_SESSION['username'])){
header("location: message.php?msg=Error:_Logout_Failed");
} else {
header("location: index.php");
exit();
}*/
header("location: index.php");
}
else {
// log potential CSRF attack.
}
unset($_SESSION["logout"]);
?>
然后,我有confirm.php
<?php
session_start();
$token= md5(uniqid());
$_SESSION['logout'] = $token;
?>
<html>
<body>
<form method="post" action="confirm_save.php">
<input type="hidden" name="token" value="<?php echo $token; ?>" />
Do you really want to log out?
<input type="submit" value=" Yes " />
<input type="button" value=" No " onclick="history.go(-1);" />
</form>
</body>
</html>
在confirm.php中点击“yes”后,出现如下错误:
Undefined index: logout in C:\wamp\www\confirm_save.php on line 4
第4行是
$token = $_SESSION['logout'];
我真的不知道可能是什么错误。
最佳答案
您通过以下方式清空您的 $_SESSION
$_SESSION = array();
然后尝试填充$token
$token = $_SESSION['logout'];
没有值(value)。
不应该有理由清空 $_SESSION,也不应该定义 $token,因为您包含 confirm.php,其中还包含您的 $token 和值
编辑
不要包含 confirm.php
但是做:
<?php
session_start();
$token = $_SESSION['logout'];
if ($_POST['token']==$token) {
...
关于php - PHP session 声明错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26477722/