【发布时间】:2015-10-02 13:11:35
【问题描述】:
我已经为此苦苦挣扎了好几个小时,但我无法让它发挥作用。当我重定向到另一个 PHP 页面时,我的所有会话变量都是空的。我在 xampp 服务器上。
session.php
<?php
session_start();
if(isset($_POST['submitted']))
{
$_SESSION['first_name'] = "MAX";
var_dump($_SESSION);
header("Location: http://localhost:8080/secure login/session2.php");
die();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859- 1" />
<title>You Logged In</title>
</head>
<body>
<form action="session.php" method="post">
<div align="center"><input type="submit" name="submit" value="Login" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
session2.php
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>You Logged In</title>
</head>
<body>
<div id="main">
<?php
echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
echo 'You are welcome to session2.php <br></br>';
if (isset($_SESSION['first_name']))
{
echo $_SESSION['first_name'] . "<br></br>";
}
else
{
echo "Your session doesn't exist. I hate php <br></br>";
echo $_SESSION['first_name'];
}
?>
</div>
</body>
</html>
会话不保存,输出是;
Array
(
)
You are welcome to session2.php
Your session doesn't exist. I hate php
Notice: Undefined index: first_name in C:\xampp\htdocs\secure login\session2.php on line 28
我尝试了其他方法,例如将会话变量的保存位置从 xampp/tmp 更改到另一个目录,但这并没有解决问题。我有一个程序,当我进行重定向时,我需要让用户保持登录状态,但这已经阻止了我超过一天。
更新:
目录之间的空间不是问题,它暂时解决了问题,但那是因为新目录还没有缓存。无论如何,又过了几天,我调试并意识到我在本地主机上运行了两个程序。两者都在使用会话,因此如果一个终止会话,它也会终止另一个会话,因为 localhost 就像一个域名并且只存在一个会话。特别是,我的其他程序的 logout.php 并没有破坏会话,而是把它弄乱了,因为你必须删除浏览器缓存才能弄乱它。我正在清空会话数组,销毁会话并销毁 cookie,这就是问题所在,所以我无法再次登录。我所要做的只是销毁会话;
【问题讨论】:
-
这段代码对我有效(好吧,我不在本地主机上,所以我不得不改变它,但除此之外一切正常)
-
看看link
-
@Matt dathew,你的网址“localhost:8080/securelogin/session2.php”是什么意思?是“安全登录”只是一个示例词?您的“session.php”的绝对 URL 是什么?
-
安全登录是我的 xampp htdocs 文件夹中的一个目录,我正在从该文件夹中编码
-
这对本地主机的任何人都有效吗?
标签: php session session-variables session-state var-dump