$ _SESSION似乎不能跨文件工作?

时间:2014-03-30 16:26:04

标签: php html css session

我正在为我的计算课做一个游戏。用户必须在播放前更改其机器人的设置。大多数人使用Visual Basic或Game Maker,但我对使用基于Web的语言感觉更舒服。

我刚刚遇到的问题是我在检查是否设置了$ _SESSION变量。这是我的脚本的截图:

http://i60.tinypic.com/2ia4hnr.jpg

正如您在左侧所见,他们是一个名为“Play”的按钮。在自定义页面上,用户填写所有设置并将它们存储为$ _SESSION变量但是当我在所有其他页面上使用If语句时,它始终将它们视为未设置,因此它不允许它们播放。

这是customise.php页码(抱歉发布了所有代码,但我觉得我需要最好地解释我的问题):

<?php $layout_context = "public"; ?>
<?php include("header.php"); ?>

<div id="main">
  <div id="navigation">
    <h2><i><u>Navigation</u></i></h2>
<br />
<u><a href="index.php">Welcome</a><br /><br />
<u><a href="instructions.php">How to Play?</a></u><br /><br />
<u><a href="customise.php"><font color="orange">Customisation</font></a></u><br /><br />
<?php if (isset($_SESSION['RobotName'], $_SESSION['traction'], $_SESSION['passenger'])) {
?>
<u><a href="game.php">Play</a></u>
<?php 
  } else {
  ?>
  <u><a href="#" onclick="alert('You must customise your robot first before playing!');">Play</a></u>
  <?php 
}
?>
  </div>
 <div id="page">
        <h1>Robot Customisation</h1>
        <br />
        <p><h3>Welcome to the BotMod Robot Customisation!</h3><br />
            Below you will have three options that enable you to play BotMod. Those are: Your Robot name, Traction Type and Passenger Size.<br /> See the bottom of this page for further guidance.<br /><br />
<br />
<form id="settings" name="settings" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <h4>Robot Name:</h4> <input type="text" name="RobotName"><br />
    <h4>Traction Type:</h4>
    <select  name="traction">
        <option value="">Please Select Your Traction Type...</option>
  <option value="Wheels">Wheels</option>
  <option value="Tracks">Tracks</option>
  <option value="Skis">Skis</option>
</select>
<br />
<h4>Passenger Size:</h4>
    <select  name="passenger">
        <option value="">Please Select Your Passenger Size...</option>
  <option value="Large">Large</option>
  <option value="Medium">Medium</option>
  <option value="Small">Small</option>
</select>
<br />
<input type="submit" name="Submit" value="Save Settings" />
</form>
<?php 
require_once("session_start.php");
// Store all submitted $_POST fields to their own $_SESSION ID to be called
// later. Must use session_start();
if (isset($_POST['Submit']) && $_POST['RobotName'] != '' && $_POST['traction'] != '' && $_POST['passenger'] != '') {
    $_SESSION['RobotName'] = $_POST['RobotName'];
    $_SESSION['traction'] = $_POST['traction'];
    $_SESSION['passenger'] = $_POST['passenger'];
} elseif (isset($_POST['Submit'])) {
    echo "<br />";
    echo "<br />";
    echo "<strong>Error: You must complete all fields on this page!</strong><br />";
}

?>
<br />
<style> 
div.container
{
width:30em;
border:1em solid;
}
div.box
{
box-sizing:border-box;
-moz-box-sizing:border-box; /* Firefox */
width: 50%;
border: 1em solid red;
float: left;
}
</style>
<?php if (isset($_POST['Submit']) && $_POST['RobotName'] != '' && $_POST['traction'] != '' && $_POST['passenger'] != '') {
echo "<h4>Your Settings (Click Play BotMod) to begin.</h4>";
?>
<div class="container">
<div class="box"><strong><i>Robot Name:</i></strong> <?php echo $_SESSION['RobotName']; ?><br /></div>
<div class="box"><strong><i>Traction:</i></strong> <?php echo $_SESSION['traction']; ?><br /></div>
<div class="box"><strong><i>Passenger:</i></strong> <?php echo $_SESSION['passenger']; ?><br /></div>
<div class="box"><strong><a href="game.php">Play BotMod</a></strong><br /></div>
<div style="clear: both;"></div>
</div>
<?php 
}
?>
  </div>
</div>

<?php include("footer.php"); ?>

如您所见,我在播放导航中创建了If语句。

<?php if (isset($_SESSION['RobotName'], $_SESSION['traction'], $_SESSION['passenger'])) {
?>
<u><a href="game.php">Play</a></u>
<?php 
  } else {
  ?>
  <u><a href="#" onclick="alert('You must customise your robot first before playing!');">Play</a></u>
  <?php 
}
?>

这不适用于任何网页:customise.php index.phpinstructions.php。 我已使用以下代码在customise.php页面上创建了变量:

If (isset($_POST['Submit']) && $_POST['RobotName'] != '' && $_POST['traction'] != '' && $_POST['passenger'] != '') {
    $_SESSION['RobotName'] = $_POST['RobotName'];
    $_SESSION['traction'] = $_POST['traction'];
    $_SESSION['passenger'] = $_POST['passenger'];
} elseif (isset($_POST['Submit'])) {
    echo "<br />";
    echo "<br />";
    echo "<strong>Error: You must complete all fields on this page!</strong><br />";
}

?>

我的index.php导航代码:

    <u><a href="index.php"><font color="orange">Welcome</font></a><br /><br />
    <u><a href="instructions.php">How to Play?</a></u><br /><br />
    <u><a href="customise.php">Customisation</a></u><br /><br />
    <?php require_once("session_start.php"); ?>
    <?php if (isset($_SESSION['RobotName'], $_SESSION['traction'], $_SESSION['passenger'])) {
    ?>
    <u><a href="game.php">Play</a></u>
    <?php 
      } else {
      ?>
<u><a href="#" onclick="alert('You must customise your robot first before playing!');">Play</a></u>
  <?php 
}
?>

所以有人知道为什么$_SESSION变量似乎不起作用?我现在正在使用MAMP,以防万一与它有关。

3 个答案:

答案 0 :(得分:2)

必须在使用session_start();变量的每个页面的顶部调用

$_SESSION

文档:http://php.net/function.session-start

答案 1 :(得分:2)

使用会话启动方法

示例:

session_start();

答案 2 :(得分:1)

您需要在访问任何会话变量之前调用session_start()