我已经挣扎了好几个小时,但我无法让它发挥作用.当我重定向到另一个
PHP页面时,我的所有会话变量都为null.我在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上运行了两个程序.两者都在使用会话,因此如果终止会话,它也会终止另一个会话,因为localhost就像一个域名,并且只存在一个会话.特别是,我的其他程序的logout.PHP并没有破坏会话,而是因为你必须删除浏览器缓存而不是它的混乱.我正在清空会话数组,破坏会话,并销毁cookie,这是问题所以我无法再次登录.我所要做的就是只破坏会话;
见 – > Killing off Global Session Variable as a logout button
好像你有问题,因为你有一个名称安全登录的空间
localhost:8080/secure%20login/session.PHP
因此,请尝试使用下划线secure_login更改名称,并更改您的代码
<?PHP
session_start();
if(isset($_POST['submitted']))
{
$_SESSION['first_name'] = "MAX";
var_dump($_SESSION);
header("Location: http://localhost:8080/secure_login/session2.PHP");
die();
}
?>