PHP 会话未正确传递整数

发布于 2024-10-19 00:34:42 字数 4734 浏览 3 评论 0原文

我正在用 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>

I'm working on a tic-tac-toe game in PHP. I'm trying to pass a session variable that will keep the user's turn with them. It passes the correct value, but it receives a different value entirely. This seems to happen whether or not it is in the Board class.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少心凉 2024-10-26 00:34:42

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

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

前两行确实取消设置会话变量,正如您所希望的那样。然而,第三行只是简单地给它重新赋值,使得前两行无效。

Every time the page loads, this code is being called:

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

The first two lines do unset the session variable, as it seems you intend. However, the third line simply re-assigns a value to it, rendering the previous two lines ineffective.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文