2017-06-15 34 views
0

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

對這個 「頭禿」 的局面任何幫助將不勝感激!

+2

你檢查過PHP關於標題的消息的錯誤日誌已發送? – RiggsFolly

+1

或將[錯誤報告](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

+0

不錯的提示,@RiggsFolly :-) – jrn

回答

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摘自: 注:

即使 session.use_tra與會話標識不通過位置標題ns_sid已啓用。它必須通過手動使用SID 不變。

您可能希望將會話ID存儲在cookie中(或通過查詢字符串傳遞它)。

+0

OUCH!查詢字符串中的SessionId?有我的會話的安全 – RiggsFolly

+0

只是說什麼是可能的;-) – jrn

+0

我不明白這是如何適用。回聲僅僅是爲了排除故障。並沒有解釋爲什麼測試會話變量持續存在,但由get實例化的sp會話變量不會持久。 – Sandy

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>