我在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。直接进入stage1.php没有任何AJAX / JavaScript。但是变量又一次丢失了。
我尝试手动输入网址。 我浏览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 :(得分:1)
从您发布的代码中不清楚,但在我看来,stage1.php不会调用调用session_start的util.php。
所以stage1.php似乎没有session_start
为避免双重包含,您可以使用include_once