会话在两个PHP文件之间不工作
我有两个php文件。第一部分是:会话在两个PHP文件之间不工作,php,html,session,Php,Html,Session,我有两个php文件。第一部分是: <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?
$city= $_POST['city'];
$movie= $_POST['movie'];
$date = $_POST['date'];
$city = stripslashes($city);
$movie = stripslashes($movie);
$date = stripslashes($date);
$_SESSION['city'] = $city;
$_SESSION['movie'] = $movie;
$_SESSION['date'] = $date;
?>
<?php
//$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("movie_booking", $con);
?>
<title>Book Ur Show</title>
<style type="text/css">
a:link {
color:#ffffff;
text-decoration: underline;
}
a:visited {
color: #ffffff;
text-decoration: underline;
}
html, body {height:100%; margin:0; padding:0;}
#page-background {position:fixed; top:0; left:0; width:100%; height:100%;}
#content {position:relative; z-index:1; padding:10px;}
</style>
</head>
<body>
<div id="page-background"><img src="images/main%20baclground.jpg" width="100%" height="100%" alt="Smile"></div>
<center>
<div class="container" style="width:800px" id="content">
<div class="header"><img src="images/logo.png" width="177" height="61" longdesc="main.php" /> <!-- end .header --></div>
<center>
<div class="content" style="background-image:url(); height:427px; color: #FFF;">
<p align="right"><?php $username = $_SESSION['myusername'];
$sql= "select * from users_tbl where username='$username' and userlevel='9'";
$result = mysql_query($sql);
if($row = mysql_fetch_array($result))
{
echo "[<a href=\"admin.php\">Admin Center</a>]";
}
?> [<a href="first.php">Main Page</a>] [<a href="logout.php">Logout</a>]</p><p align="left"><?php
$username = $_SESSION['myusername'];
echo "Welcome $username";
?></p>
<form name="form1" action="book.php" method="post">
<table width="200" border="0">
<tr>
<td>City</td>
<td><input name="city" type="text" id="city" readonly="true" style="background-color:#000; color:#FFF" value="<? $sql="Select * from city where city_id='$city'";$sqlresult=mysql_query($sql);$row = mysql_fetch_array($sqlresult);$cityname=$row['city_name'];echo $cityname;?>" /></td>
</tr>
<tr>
<td>Movie</td>
<td><input name="movie" type="text" id="movie" readonly="true" style="background-color:#000; color:#FFF" value="<? $sql="Select * from movie where movie_id='$movie'" ;$sqlresult=mysql_query($sql);$row = mysql_fetch_array($sqlresult);$moviename=$row['movie_name'];echo $moviename;?>" /> </td>
</tr>
<tr>
<td>Date</td>
<?php echo var_dump($_SESSION['city']); ?>
<td><input name="date" type="text" id="date" readonly="true" style="background-color:#000; color:#FFF" value="<? $sql="Select * from movie where date='$date' and movie_id='$movie' and city_id='$city'";$sqlresult=mysql_query($sql);$row = mysql_fetch_array($sqlresult);$date2=$row['date'];echo $date2;?>" /></td>
</tr>
</table>
<?php
echo "<br><br>";
//Art of MySQL
$sql = "Select movie_name,theatre_id, date, showtiming from movie where 1";
//Check if movie is not null
if( strlen($movie)>0 )
{
$sql.= " and movie_id = $movie ";
}
if( strlen($city)>0 )
{
$sql.= " and city_id = '$city' ";
}
if( strlen($date)>0 )
{
$sql.= " and date='$date' ";
}
$result = mysql_query($sql);
echo "<table>";
echo "<tr>
<td width=\"100px\">Movie</td>
<td width=\"100px\"> Theatre</td>
<td width=\"100px\"> Date </td>
<td width=\"100px\">Show Timing</td>
<td width=\"100px\">Book Ticket</td></b>
</tr>";
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while($row = mysql_fetch_array($result))
{
echo "<form name=\"form1\" action=\"book.php\" method=\"post\">";
$sql2 = "Select theatre_name from theatre where theatre_id=".$row['theatre_id']."";
$result2 = mysql_query($sql2);
$row2 = mysql_fetch_array($result2);
$mname = $row['movie_name'];
$tname = $row2['theatre_name'];
$stime = $row['showtiming'];
$date = $row['date'];
echo "<tr>
<td><input name=\"mname\" type=\"text\" id=\"mname\" readonly=\"true\" style=\"background-color:#000; color:#FFF\" value='$mname'/></td>
<td><input name=\"tname\" type=\"text\" id=\"tname\" readonly=\"true\" style=\"background-color:#000; color:#FFF\" value='$tname'/></td>
<td><input name=\"date\" type=\"text\" id=\"date\" readonly=\"true\" style=\"background-color:#000; color:#FFF\" value='$date'/></td>
<td><input name=\"stime\" type=\"text\" id=\"stime\" readonly=\"true\" style=\"background-color:#000; color:#FFF\" value='$stime'/></td>
<td align=\"center\"><input name=\"book\" type=\"submit\" value=\"Book\" /></td>
</tr>";
echo "</form>";
}
echo "</table>";
?>
</form>
</div>
</center>
</body>
</html>
输出是
heyis set
知道我为什么可以访问$\u会话中的数据吗。谢谢你的建议
编辑
正如亚历克斯所要求的
我添加到两个文件中。在最后一行的第一个文件中,我添加了
<?php echo var_dump($_SESSION); ?>
但在第二个文件中我得到了
array(8) { ["myusername"]=> string(6) "bkg988" ["password"]=> string(32) "827ccb0eea8a706c4c34a16891f84e7b" ["city"]=> string(0) "" ["movie"]=> string(0) "" ["date"]=> string(0) "" ["data"]=> string(27) "you have an active session!" ["stime"]=> string(0) "" ["tname"]=> string(0) "" }
尝试执行以下操作,以查看是否有变量
$city= $_POST['city'];
$movie= $_POST['movie'];
$date = $_POST['date'];
,或者使用$\u REQUEST
而不是$\u POST
问题在于(通过您与我们的进一步调试)第一个文件顶部的这段代码
<?
$city= $_POST['city'];
$movie= $_POST['movie'];
$date = $_POST['date'];
$city = stripslashes($city);
$movie = stripslashes($movie);
$date = stripslashes($date);
$_SESSION['city'] = $city;
$_SESSION['movie'] = $movie;
$_SESSION['date'] = $date;
?>
其次,在使用变量之前检查变量是否存在-检查$\u POST
变量(包括$\u POST本身)在使用之前是否已设置
if(isset($_POST))
{
if(isset($_POST['city']))
{
$_SESSION['city'] = stripslashes($_POST['city']);
}
if(isset($_POST['movie']))
{
$_SESSION['movie'] = stripslashes($_POST['movie']);
}
if(isset($_POST['date']))
{
$_SESSION['date'] = stripslashes($_POST['date']);
}
}
我从第一个文件中收到了这些数据,但问题是我需要初始化会话变量以在将来使用它们。如果你
echo
第二个文件中的变量(你要在其中检索会话)并获取数据,则可以。否则您还没有注册会话变量
<?
$city= $_POST['city'];
$movie= $_POST['movie'];
$date = $_POST['date'];
$city = stripslashes($city);
$movie = stripslashes($movie);
$date = stripslashes($date);
$_SESSION['city'] = $city;
$_SESSION['movie'] = $movie;
$_SESSION['date'] = $date;
?>
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
if(isset($_POST))
{
if(isset($_POST['city']))
{
$_SESSION['city'] = stripslashes($_POST['city']);
}
if(isset($_POST['movie']))
{
$_SESSION['movie'] = stripslashes($_POST['movie']);
}
if(isset($_POST['date']))
{
$_SESSION['date'] = stripslashes($_POST['date']);
}
}