2011-02-22 42 views
0

我正在使用PHP进行井字游戏。我试图传递一个会保持用户轮到他们的会话变量。它传递正确的值,但它完全收到一个不同的值。无论是否属于理事会类别,这似乎都会发生。PHP会话不能正确传递整数

<?php 
ini_set('display_errors', 'On'); 
error_reporting(E_ALL); 
include('./board.php'); 
session_start(); 
if(isset($_POST['resetgame'])){ 
    session_destroy(); 
    session_start(); 
} 
if(isset($_SESSION['turn'])){ 
    $turn=$_SESSION['turn']; 
} 
else{ 
    echo "This should only happen once."; 
    $turn=2; 
} 


if(isset($_POST['p'])){ 
    if($_POST['p']==1){ 
     if(isset($_POST['name1'])){ 
      $board=new Board(1,$_POST['name1']); 
     } 
     else 
      $board=new Board(1); 
    } 
    else{ 
     if(isset($_POST['name1'])&&isset($_POST['name2'])){ 
      $board=new Board(2,$_POST['name1'],$_POST['name2']); 
     } 
    } 

     $_SESSION['startup']=1; 
    } 
    else{ 
     if(!isset($_SESSION['board'])){ 
    $newgame=1; 
} 
else{ 
    $newgame=0; 
    $board=$_SESSION['board']; 
    echo "THE ORIGINAL SESSION:".$board->getTurn(); 
} 
} 
for($i=0;$i<=2;$i++){ 
    for($j=0;$j<=2;$j++){ 
     if(isset($_POST[$i.','.$j])){ 
      $board->setSquare($i,$j,$turn);  
      //echo $board->gameboard[$i][$j]; 
     } 
    } 
} 
?> 
<!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" /> 
<title>Tic-Tac-Toe</title> 


    <div class="content"> 
    <h1>Tic-Tac-Toe</h1> 
    <? 
    if($newgame==1){ 
    echo "<p>Welcome to Tic-Tac-Toe! Please choose number of players!<br /> 
    </p>"; ?> 
    <form action="./tic-tac-toe.php" name="newgamef" method="post" id="newgamef"> 
    <label> 
    <button name="pd" id="p" value="Single Player" onclick="players(1)" type="button">Single Player</button> 

    </label> 
    <label> 
    <button name="pd" value="Two Players" onclick="players(2)" type="button">Two Players </button><br /> 

</label> 
    <div align="center" id="name1" style="visibility:hidden;position:fixed"> 
    <label>Player 1's Name (Optional): 
    <input type="text" name="name1" /> 
    </label><br /> 

    </div><br /> 

    <div align="center" id="name2" style="visibility:hidden;position:fixed"> 
    <label>Player 2's Name (Optional): 
    <input type="text" name="name2" /> 
    </label><br /> 

    </div><br /> 

    <div align="center" id="submit" style="visibility:hidden;position:fixed"> 

    </div><br /> 
    </form> 
    <? echo " ";} 
    if(isset($board)){ 
     echo ""; 
    // echo $board->turn; 
     unset($_SESSION['board']); 
     $_SESSION['board']=$board; 
     $_SESSION['turn']=null; 
     unset($_SESSION['turn']); 
     $_SESSION['turn']=$turn; 
     if($board->hasWon()==3){ 
     ?> 
     <form action="./tic-tac-toe.php" method="post"> 
    <p>&nbsp;</p> 
    <table width="200" border="0"> 
     <tr> 
     <td><? echo $board->gb(0,0) ;?></td> 
     <td><? echo $board->gb(0,1) ;?></td> 
     <td><? echo $board->gb(0,2) ;?></td> 
     </tr> 
     <tr> 
     <td><? echo $board->gb(1,0) ;?></td> 
     <td><? echo $board->gb(1,1) ;?></td> 
     <td><? echo $board->gb(1,2) ;?></td> 
     </tr> 
     <tr> 
     <td><? echo $board->gb(2,0) ;?></td> 
     <td><? echo $board->gb(2,1) ;?></td> 
     <td><? echo $board->gb(2,2) ;?></td> 
     </tr> 
    </table> 
    </form> 
    <? 
     } 
     else if($board->hasWon()==1){ 
      echo "The X's Win!"; 
     } 
     else if($board->hasWon()==2){ 
      echo "The O's Win!"; 
     } 

     echo ""; 
    } 
    ?> 
    <form action="./tic-tac-toe.php" method="post"> 
    <button type="submit" name="resetgame" value="resetgame">New Game?</button> 
    </form> 
    <!-- end .content --></div> 
    <div class="footer"> 
    <p>This .footer contains the declaration position:relative; to give Internet Explorer 6 hasLayout for the .footer and cause it to clear correctly. If you're not required to support IE6, you may remove it.</p> 
    <!-- end .footer --></div> 
<!-- end .container --></div> 
</body> 
</html> 
+0

该示例中的几个变量中的哪一个给你提出问题? – 2011-02-22 21:25:01

+0

它是$ _SESSION ['turn']和$ turn。 – Laxsnor 2011-02-22 21:28:11

回答

1

每次页面加载,这段代码被称为:

$_SESSION['turn']=null; 
    unset($_SESSION['turn']); 
    $_SESSION['turn']=$turn; 

前两行做未设置会话变量,因为它似乎你打算。但是,第三行只是重新给它赋值,使前两行无效。

+0

是的,但这些应该在改变转向变量后生效。 – Laxsnor 2011-02-23 05:19:58