【问题标题】:PHP Session Not Correctly Passing an IntegerPHP 会话未正确传递整数
【发布时间】:2011-02-22 21:18:29
【问题描述】:

我正在使用 PHP 开发井字游戏。我正在尝试传递一个会话变量,让用户轮到他们。它传递了正确的值,但它完全接收到不同的值。无论是否在 Board 类中,这似乎都会发生。

   <?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>

【问题讨论】:

  • 示例中的几个变量中的哪一个给您带来了问题?
  • 现在是 $_SESSION['turn'] 和 $turn。

标签: php session session-variables


【解决方案1】:

每次页面加载时,都会调用这段代码:

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

如您所愿,前两行确实取消了会话变量的设置。但是,第三行只是简单地给它重新赋值,导致前两行无效。

【讨论】:

  • 是的,但是这些应该在turn变量更改后生效。
猜你喜欢
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 2021-02-21
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
  • 2013-03-24
相关资源
最近更新 更多