簡體   English   中英

Session在兩個PHP文件之間不起作用

[英]Session is not working between two PHP files

我有兩個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 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>

我已經檢查過$_SESSION['city']已正確設置,當用戶稍后提交表單時,它會調用另一個php文件:

<?php
session_start();
$city = $_SESSION['city'];
echo "hey".$_SESSION['city'];
if(isset($_SESSION['city'])){
    echo "is set";
}
?>

輸出是

heyis set

知道為什么我可以訪問$ _session里面的數據。 欣賞任何想法。

編輯

正如Alex要求的那樣

我添加到這兩個文件。 在最后一行的第一個文件中我添加了

<?php echo var_dump($_SESSION); ?>

我明白了

 array(8) { ["myusername"]=> string(6) "bkg988" ["password"]=> string(32) "827ccb0eea8a706c4c34a16891f84e7b" ["city"]=> string(1) "1" ["movie"]=> string(0) "" ["date"]=> string(0) "" ["data"]=> string(27) "you have an active session!" ["stime"]=> string(0) "" ["tname"]=> string(0) "" }

但在第二個檔案中,我得到了

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'];

,或使用$_REQUEST而不是$_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;
?>

當您將表單發布到該頁面時,所發生的一切都按預期工作。 這些值已分配,當您檢查底部的$_SESSION變量時它很好。

如果然后使用$_POST數據再次瀏覽到該頁面,因為$_POST為空,您將分配$_SESSION變量,即不存在的變量的值。

所以解決方案分為兩部分。

首先是錯誤報告 - 打開錯誤報告。 在開發時,你應該總是有錯誤報告(在你試圖使用不存在的$ _POST變量的情況下,這會拋出警告)

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);

其次,在使用它們之前檢查變量是否存在 - 在使用它們之前檢查$_POST變量(包括$ _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']);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM