【发布时间】:2014-04-03 01:49:50
【问题描述】:
我在 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 可以更改中间 div,效果很好。如果用户选择“开始新订单”,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() 命令。另请注意,这是一个开发服务器。我的是网站上唯一的会话。有什么想法吗?
【问题讨论】:
-
查看 session_start() 文档中的一些 cmets。听起来你适合那里的条目之一:us2.php.net/manual/en/function.session-start.php