2014-03-30 114 views
0

我正在爲我的計算課做一個遊戲。用戶在播放之前必須更改其機器人的設置。大多數使用的是Visual Basic或Game Maker,但我對使用基於Web的語言感覺更加舒適。

我剛剛遇到的問題是當我檢查是否設置$ _SESSION變量。這是我的腳本的截圖:

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

正如你可以看到左邊的是一個名爲「播放」按鈕。在用戶自定義頁面中,用戶填寫所有設置,並將它們存儲爲$ _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.phpindex.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,以防與此有關。

+0

你使用'在session_start()'每一頁 –

+0

是的,我需要的文件上'session_start.php'在所有包含此代碼的文件中:'<?php session_start(); ?>'。 –

+0

@MattKent它不能是'session start.php'它可以是'session-start.php'或'session_start.php';)試着在第一行放上'<?php session_start()?>'你的文檔使用會話變量 – mdesdev

回答

2

session_start();必須在每一個地方,你使用$_SESSION變量頁面的頂部被調用之前,調用session_start()。

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

+0

這是我的問題。我沒有在頁面的頂部定義它。 :) –

1

您需要訪問任何會話變量

2

使用會話啓動方法

例如:

session_start();