我在许多论坛上搜索过有关此问题的许多答案。许多国家确定使用session_start()
其他人处理托管平台,甚至有人说他们使用$_GET
,会话变量没有持续超出第一页,但没有明确的解决方案,似乎符合我的情况。
我已经调试到了这一点,我知道它在$_GET
和做同样的事情,如果我使用$_REQUEST
(这里没有帖子),所以我张贴希望得到一些解决方案问题。这里是我的代码,简化了发布的目的,但它确实工作并演示$_SESSION['test']
变量如何持续存在,但$_GET
实例化的变量$_SESSION['sp']
没有。
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>
对这个 “头秃” 的局面任何帮助将不胜感激!
你检查过PHP关于标题的消息的错误日志已发送? – RiggsFolly
或将[错误报告](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025)添加到文件顶部的 (s) _开始测试_刚刚打开PHP标记后,例如 '<?php error_reporting(E_ALL); ini_set('display_errors',1);'看看它是否产生任何东西。 – RiggsFolly
不错的提示,@RiggsFolly :-) – jrn