2017-06-15 46 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>