2014-03-02 80 views
0

我在PHP中失去會話變量時遇到了很多麻煩。我發現了很多很好的信息,但在這種情況下沒有任何幫助。這裏是我的PHP文件:PHP會話變量在移動到頁面後丟失

首先,util.php。它包含在每個文件的頂部。它有session_start()命令和MySQL連接字符串:

<?php 
    session_start(); 
    $connect=mysqli_connect("localhost","mysql","mypassword","mydb"); 
    if(mysqli_connect_errno()){ 
     echo 'Failed to connect to database ' . mysqli_connect_error(); 
    } 
?> 

接下來,show_variables.php。它將所有會話變量轉儲到屏幕上:

<?php 
echo date('H:i:s'); 
echo "<br/><p>Session Variables:</p>"; 
foreach ($_SESSION as $key=>$val) 
    echo $key." ".$val."<br/>"; 
echo session_name(); 
?> 

現在第一頁,index.php。頁面頂部從數據庫表中抽取員工姓名。正文提供一個包含員工姓名和兩個角色的表單。該頁面將根據所選角色重定向。

<?php 
    include "util.php"; 
    $query=mysqli_query($connect,"select * from staff"); 
    while ($row = mysqli_fetch_array($query)){ 
     $staff_select .= '<option value="'.$row[id].'">'.$row[name_first].' '.$row[name_last].'</option>'; 
    } 
?> 
<html> 
<head> 
    <title>Restaurant Order Tracking</title> 
    <link href="./css/style.css" rel="stylesheet" type="text/css"> 
    <script language="javascript" type="text/javascript"> 
     function start_shift(){ 
      var e_select = document.selector.employee_select; 
      var name = e_select.options[e_select.selectedIndex].value; 
      var r_select = document.selector.role_select; 
      var role = r_select.options[r_select.selectedIndex].value; 
      alert("Ok, " + name + ". You're a " + role + " today. Let's get to work."); 
      if(role == "server"){location = "./server/server.php?id=" + name}; 
      if(role == "foodprep"){location = "foodprep.php?id=" + name}; 
     } 
    </script> 
</head> 
<body> 
    <div id="header"> 
     <h1>Welcome To The Restaurant Order Tracker</h1> 
    </div> 
    <div id="middle"> 
     <p>Now get to work !</p> 
     <form name="selector"> 
      <p>Select your employee:</p> 
      <select name="employee_select"> 
       <?php 
        echo $staff_select; 
       ?> 
      </select> 
      <p>Select your role for today</p> 
      <select name="role_select"> 
       <option value="server">Server</option> 
       <option value="foodprep">Food Prep</option> 
      </select> 
      <br/> 
      <input type="button" value="Start Shift" onclick="start_shift()"/> 
     </form> 
    <?php include "show_variables.php";?> 
    </div> 
</body> 
</html> 

此頁面正常工作。在這裏選擇服務器會導致「server.php」。以下是該文件:

<?php 
    include "../util.php"; 
    $_SESSION['id']=$id=$_GET['id']; 
    $_SESSION['foo']="fubar"; 
    $query = mysqli_query($connect,"select name_first, name_last from staff where id = $id"); 
    while($row = mysqli_fetch_array($query)){ 
     $_SESSION['name'] = $row['name_first'].' '.$row['name_last']; 
     } 
?> 
<html> 
<head> 
    <Title>Server Interface</Title> 
    <link href="../css/style.css" rel="stylesheet" type="text/css"> 
    <script language="JavaScript" type="text/javascript"> 
     function update(target){ 
      var ajaxRequest; 
      ajaxRequest = new XMLHttpRequest(); 
      ajaxRequest.onreadystatechange = function(){ 
       if(ajaxRequest.readyState == 4){ 
        var middleDisplay = document.getElementById('middle'); 
        middleDisplay.innerHTML = ajaxRequest.responseText; 
        } 
       } 
      ajaxRequest.open("GET", target, true); 
      ajaxRequest.send(null); 
     } 
    </script> 
</head> 
<body> 
    <div id="header"> 
     <p>Server Interface</p> 
     <p>Server Name: <?php echo $_SESSION['name'] ?> </p> 
     <form action="../logout.php" method="post"> 
     <input type="submit" value="Logout"> 
     </form> 
    </div> 
    <div id="middle"> 
     <?php include "stage1.php" ?> 
    </div> 
</body> 
</html> 

本頁設置了三個變量:id,name和foo。正如所料,show_variable.php具有以下的輸出:

19:54:34 
Session Variables: 
id 2 
foo fubar 
name Dave Smith 
PHPSESSID 

還有一個包括中間DIV stage1.php。這是那個文件:

<br/> 
<form name="selector"> 
    <input type="button" value="Start a new order" name="new_order" onclick = "update('new_order.php')" /> 
    <input type="button" value="View your open orders" name="view_order" onclick="update('view_order.php')" /> 
</form> 
<?php include "../show_variables.php"; ?> 

到目前爲止,這麼好。有一些AJAX可以改變中間格,這很好。如果用戶選擇「開始新訂單」,AJAX會將new_order.php放入中間div。以下是文件:

<?php 
    include "../util.php"; 
    $query=mysqli_query($connect,"select * from loc"); 
    while ($row = mysqli_fetch_array($query)){ 
     $table_select .= '<option value="'.$row[id].'">'.$row[description].'</option>'; 
    } 
    echo "<br/> 
    <form name = 'new_order'> 
    <label>Enter the table number for this order</label> 
     <select name = 'table_select'> 
      $table_select 
     </select> 
     <br/> 
     <input type='button' value='Start New Order' onclick=\"update('enter_order.php?table=' + document.new_order.table_select[document.new_order.table_select.selectedIndex].value)\" /> 
     <br/> 
     <input type='button' value='Cancel New Order' onclick=\"update('stage1.php')\" /> 
    </form> 
<a href= "stage1.php">Go back to stage 1 via link</a> 
" 
?> 
<?php include "../show_variables.php"; ?> 

該頁面還顯示了正確的會話變量:

19:55:27 
Session Variables: 
id 2 
foo fubar 
name Dave Smith 
PHPSESSID 

但在那之後,所有的變量消失。如果用戶點擊「取消新訂單」,AJAX腳本再次用stage1.php填充中間div。會話變量不再存在:

19:59:12 
Session Variables: 
PHPSESSID 

這不能是stage1.php文件中的拼寫錯誤。完全相同的代碼在一次查看中具有會話變量,但在另一次查看時會丟失它們。 我想也許AJAX搞砸了我,所以我添加了href到new_order.php。沒有任何AJAX/JavaScript就直接進入了stage1.php。但變數再次失去了。

我嘗試手動輸入網址。 我瀏覽到http://www.mysite.com/server/server.php?id=1。會話變量顯示正確。 我打開一個新標籤並瀏覽到http://www.mysite.com/server/server.php(沒有獲得)。會話變量存在且設置相同。 我打開另一個選項卡並瀏覽至http://www.mysite.com/server/stage1.php。會話變量不存在。

請注意,每個文件都通過util.php文件具有session_start()命令。還要注意這是一個開發服務器。我的網站是唯一的會話。有任何想法嗎?

+0

結帳一些意見的文件在在session_start()。聽起來像你適合那裏的條目之一:http://us2.php.net/manual/en/function.session-start.php – Zarathuztra

回答

1

從您發佈的代碼中不清楚,但在我看來,stage1.php不會調用調用session_start的util.php。

所以stage1.php似乎沒有在session_start

爲了避免雙重包括,你可以應該使用include_once

+0

啊,就是這樣。 stage1.php第一次被包含(粘貼)在server.php中,因此它只是server.php的一部分。但是,當AJAX腳本顯式調用stage1.php時,它的行爲就像是一個獨立的文件。因此,不會執行server.php中的「include util.php」命令。謝謝。不僅僅是發現錯字,你已經幫助我更好地理解PHP的邏輯。 – Bagheera