我在許多論壇上搜索過有關此問題的許多答案。許多國家確定使用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