我在很多论坛上搜索过很多关于这个问题的答案。许多人声明要确保使用session_start()
其他人处理托管平台,甚至有人声称他们使用过$_GET
并且会话变量没有超出第一页,但是没有明确的解决方案似乎匹配我的方案。
我已经调试了它,我知道它在$_GET
并且如果我使用$_REQUEST
做同样的事情(此时没有帖子),所以我在发帖希望能够解决这个问题。以下是我的代码,为了发布目的而进行了简化,但它确实有用,并演示$_SESSION['test']
变量如何保留,但$_SESSION['sp']
实例化的$_GET
变量不会。
index2.php
<?php
/*some comments*/
session_start();
session_unset();
//define some variables here
?>
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
<table>
<tr>
<td width-"750">
<p>
Make a choice:<br><br>
<a href="first_page.php?page=third_page">Get the third page after first and second pages</a><br>
<a href="first_page.php?page=fourth_page">Get the fourth page after first and second pages</a><br>
</p>
</td>
</tr>
</table>
</body>
</html>
first_page.php
<?php
/*some comments*/
session_start();
$s=$_GET['page'];
$_SESSION['sp']=$s;
$_SESSION['test']="test123";
echo "s variable from get is set to " . $s . "<br>";
echo "session variable for page is set to " . $_SESSION['sp'] . "<br>";
echo "session variable for test is set to " . $_SESSION['test'] . "<br>";
if (isset($_POST['submit']) && !empty($_POST['submit']))
{
header('Location: second_page.php');
}
?>
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
<table>
<tr>
<td width="750px">
<form method="post" action='first_page.php'>
<p>
HTML text and a select tag here for choices
</p>
<input type="submit" name="submit" value="Submit">
</form>
</td>
</tr>
</table>
</body>
</html>
second_page.php
<?php
/*some comments*/
session_start();
echo "session variable for page is set to " . $_SESSION['sp'] . "<br>";
echo "session variable for test is set to " . $_SESSION['test'] . "<br>";
//Test if submit button named submit was clicked and not empty
if (isset($_POST['submit']) && !empty($_POST['submit']))
{
if($_SESSION['sp']=="third_page") {
header('Location: third_page.php');
}
if($_SESSION['sp']=="fourth_page") {
header('Location: fourth_page.php');
} else {
header('Location: index2.php');
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
<table>
<tr>
<td width="750px">
<form method="post" action='second_page.php'>
<p>
HTML text and a select tag here for choices
</p>
<input type="submit" name="submit" value="Submit">
</form>
</td>
</tr>
</table>
</body>
</html>
对于这种“头部秃顶”情况的任何帮助都将非常感谢!!
答案 0 :(得分:0)
一旦输出已经发送,您就无法修改header()
,例如在你的情况下通过echo
。
您可能希望在first_page.php中注释掉这些行
echo "s variable from get is set to " . $s . "<br>";
echo "session variable for page is set to " . $_SESSION['sp'] . "<br>";
echo "session variable for test is set to " . $_SESSION['test'] . "<br>";
和second_page.php中的这些:
echo "session variable for page is set to " . $_SESSION['sp'] . "<br>";
echo "session variable for test is set to " . $_SESSION['test'] . "<br>";
取自php.org: 注意:
即使是,也不会使用Location标头传递会话ID session.use_trans_sid已启用。它必须通过SID手动传递 恒定。
您可能希望将会话ID存储在cookie中(或通过查询字符串传递它)。
答案 1 :(得分:0)
<强> 解决: 强>
感谢大家的帮助!
在重定向到second_page之前,表单操作再次调用first_page,并且在提交表单时,get是空白的,这是有道理的。以下是其他人遇到此问题的解决方案。我已经离开了一些调试来帮助别人以及它帮助了我。
index2.php -
<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
<table>
<tr>
<td width-"750">
<p>
Make a choice:<br><br>
<a href="first_page.php?page=third_page">Get the third page after first and second pages</a><br>
<a href="first_page.php?page=fourth_page">Get the fourth page after first and second pages</a><br>
</p>
</td>
</tr>
</table>
</body>
</html>
first_page.php -
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
session_start();
// is the page parameter present at all?
if (!isset($_GET['page']))
{
http_response_code(400);
exit('Missing URL parameter: page');
}
/* is the parameter valid? - this will fail if the page= is not all numeric
if (!ctype_digit($_GET['page']) || $_GET['page'] == 0)
{
http_response_code(400);
exit('Invalid URL parameter: page');
}
*/
$s=$_GET['page'];
$_SESSION['sp']=$s;
$_SESSION['test']="test123";
echo "s variable from get is set to " . $s . "<br>";
echo "session variable for page is set to " . $_SESSION['sp'] . "<br>";
echo "session variable for test is set to " . $_SESSION['test'] . "<br>";
if (isset($_POST['submit']) && !empty($_POST['submit']))
{
header('Location: second_page.php');
}
?>
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
<table>
<tr>
<td width="750px">
<form method="post" **action="first_page.php?page=<?php echo $_SESSION['sp']?>">**
<p>
HTML text and a select tag here for choices
</p>
<input type="submit" name="submit" value="Submit">
</form>
</td>
</tr>
</table>
</body>
</html>
second_page.php -
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
session_start();
echo "session variable for page is set to " . $_SESSION['sp'] . "<br>";
echo "session variable for test is set to " . $_SESSION['test'] . "<br>";
if (isset($_POST['submit']) && !empty($_POST['submit']))
{
if($_SESSION['sp']=="third_page") {
header('Location: third_page.php');
} else {
if($_SESSION['sp']=="fourth_page") {
header('Location: fourth_page.php');
} else {
header('Location: index2.php');
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
<table>
<tr>
<td width="750px">
<form method="post" action="second_page.php">
<p>
HTML text and a select tag here for choices
</p>
<input type="submit" name="submit" value="Submit">
</form>
</td>
</tr>
</table>
</body>
</html>
third_page.php -
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
Congrats, you made it to the third page!
</body>
</html>
fourth_page.php -
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="includes/style.css">
</head>
<body>
Congrats, you made it to the fourth page!
</body>
</html>