取消设置过早触发并取消之前的操作

时间:2018-03-09 17:51:21

标签: php

我试图为一个简单的游戏创建一些代码 - 选择箭头落地的位置,然后选择计算机&#34;射击&#34;它然后显示&#34;做得好&#34;或者&#34;没有它落在别处&#34;消息,带有箭头的图像着陆(一次,刷新应使它们消失)。显然正在使用正在发生的POST重定向,但是对于SESSION变量的未设置似乎并不重要,它似乎过早取消,这意味着消息根本不显示。< / p>

在线搜索,我一直无法看到错误(SESSION正在进行,重定向正在运行,但无论在哪里取消设置()都取消设置并且根本不显示,它和&# #39; s可能很简单,所以会很高兴看到我的代码(我已经在我的各种尝试中留下了注释,所以你可以看到问题):

<?php
session_start();

function GuessArrowGame(){

    if (isset ($_POST['YourArrow'])){
        //Create array of possible answers
        $possibleScores = array ("miss","1","3","5","7","9");
        //print_r ($possibleScores);

        //Check output from dropdown
        //echo "POST is:";
        //print_r ($_POST);

        //Get _POST and make into a string
        $YourArrowLands = $_POST['YourArrow'];
        $SESSION['YourArrowLands'] = $YourArrowLands;
        //echo "Your arrow Lands \n";
        //print_r ($SESSION['YourArrowLands']);
        //echo "\n";
        //echo "\n";


        //Randomise array
        shuffle ($possibleScores);
        //print_r ($possibleScores);

        //Make variable from randomised array
        $ArrowLands = $possibleScores['0'];
        //echo "Arrow Lands: $ArrowLands \n";


        //Make Image from each arrow landing
        $ArrowLandsImage="0";

        if ($ArrowLands=="9"){
            $ArrowLandsImage="Gold.png";
        }elseif($ArrowLands=="7"){
            $ArrowLandsImage="Red.png";
        }elseif($ArrowLands=="5"){
            $ArrowLandsImage="Blue.png";
        }elseif($ArrowLands=="3"){
            $ArrowLandsImage="Black.png";
        }elseif($ArrowLands=="1"){
            $ArrowLandsImage="White.png";
        }elseif($ArrowLands=="miss"){
            $ArrowLandsImage="Miss.png";
        }
        echo "<p>Arrowlandsimage is ".$ArrowLandsImage."</p>\n";
        $_SESSION['ArrowImage'] = $ArrowLandsImage;
        echo $_SESSION['ArrowImage'];

        //check score against arrow
        if ($SESSION['YourArrowLands'] == $ArrowLands){
            $_SESSION['message'] = "Great job, you guessed right, you scored ".$ArrowLands.".\n";
        }else{
            $_SESSION['message'] = "Sorry, you guessed $YourArrowLands, but you scored ".$ArrowLands.".\n";
        }


        //Display image of arrow Landing
        //$SESSION['ArrowImage'] = $ArrowLandsImage;        


        header ("Location:Game1.php");
        return;
    }
};  


print_r ($_SESSION);
function flashMessages(){

    if (isset($_SESSION['ArrowImage'])) {
  echo $_SESSION['ArrowImage'];
  //unset($_SESSION['ArrowImage']);
}




    if ( isset ($_SESSION['message']) ) {
        echo("<p>".$_SESSION['message']."</p>\n");
        //unset($_SESSION['success']);
    }
    if ( isset ($_SESSION['ArrowImage']) ) {
echo '<img src="'.$_SESSION['ArrowImage'].'" title="Arrow Lands" alt="Arrow Lands" />';         
        echo "\n";
//      unset($_SESSION['ArrowImage']);
    }
}

        ?>
<!DOCTYPE html>
<html lang="en">
<head>
      <meta charset="UTF-8">
      <title>Evolution Archers</title>
      <!--<link rel="icon" type="image/x-icon" href="Images/EvoLogo.ico"/>
      <link rel="stylesheet" href="CSS/Style.css">
      <script src="JS/Script.js"></script>-->
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body id="top">
<!--<img  id="MenuShow" class="showLink" onclick="showHide('Menu');return false;" src="Images/Menu.png" alt="Menu Show">
<div id="Menu" class="more">
<img  id="MenuHide" class="hideLink" onclick="showHide('Menu');return false;" src="Images/MenuEnd.png" alt="Menu Hide">
</div>
      <nav class = "nav" id="nav">
            <a class="home" href='Index.html'>
              <img src="Images/EvoWebPageTopTransparent.png" alt="Home">
             </a>
                <a href='History.html'>History of the Club</a>
                <a href='Gallery.html'>Gallery</a>
                <a href='Upcoming.html'>Upcoming Events</a>
                <a href='Past.html'>Past Events</a>
                <a href='Beginners.html'>Beginners</a>
                <a href='LYG.html'>London Youth Games</a>
                <a href='Members.html'>Members Area</a>
                <a class="active" href='Useful.html'>Useful Stuff</a>
                <a href='Rules.html'>Rules and Etiquette</a>

        </nav>-->
                <div class=MainPage>

    <section>
        <h1>Can you hit the Gold?</h1>

        <!--Insert Code for clicky button here*/-->
        <form method="post">
        <p>
        <label for="GuessArrow">Where will your arrow land?</label>
        <select name="YourArrow" id="GuessArrow">
        <option value="9">9 - Gold</option>
        <option value="7">7 - Red</option>
        <option value="5">5 - Blue</option>
        <option value="3">3 - Black</option>
        <option value="1">1 - WhiteGold</option>
        <option value="miss">m - Miss</option>
        </select>
        </p>
        <input type="submit"/>
        </form>

        <?php
        GuessArrowGame();

        flashmessages();

    //$message = isset($_SESSION['message']) ? $_SESSION['message'] : false;
    //$imageShow = isset($_SESSION['ArrowImage']) ? $_SESSION['ArrowImage'] : false;

       /*if ( $message !== false )  {
        echo("<p>$message</p>\n");
 // unset($_SESSION['message']);
   }*/
        //Display image of arrow Landing
        /*if ($imageShow !== false){
            echo '<img src="'.$imageShow.'" title="Arrow Lands" alt="Arrow Lands" />';          
        echo "\n";
    //unset($_SESSION['ArrowImage']);
        }   */  

    /*  if (isset ($_SESSION['message'])){
    echo ("<p>".$_SESSION['message']."</p>\n");
    //echo ("printing check");
    unset($_SESSION['message']);

}*/

        ?>

<?php
//  echo($_SESSION['ArrowImage']);

    //unset($_SESSION['ArrowImage']);
    //unset($_SESSION['message']);
?>


        </section>
  <footer>
           <p>All content &copy; Evolution Archers 2004-2018 or individual members of Evolution Archers.</p>
           <p>Evolution Archers are not responsible for the content of other websites.</p>
           <p>Links to other websites do not indicate any association between Evolution Archers and other websites.</p>

      </footer>
      </div>
</body>
</html>

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

问题是,您希望在重定向并显示一次后清除您在$ _SESSION中设置的消息。然而,这不是它的作用,因此:

您的代码设置了重定向,但继续执行整页(以及取消设置)。然后它重定向回自身。当然你看到$ _SESSION变量是空的。

要在代码中修复此问题,请按以下步骤操作...

  1. 在开始输出GuessArrowGame();之前将<!DOCTYPE html>...移至
  2. function GuessArrowGame(){}
  3. 之前session_write_close();内添加header("Location:Game1.php");
  4. function GuessArrowGame(){}
  5. 之后exit;内添加header("Location:Game1.php");
  6. 将所有$SESSION修复为您遇到的$_SESSION
  7. 将所有unset(或只是$_SESSION=[];)放在代码底部。
  8. 清除评论并添加以上内容后的代码:

    <?php
    session_start();
    
    function GuessArrowGame() {
        if (isset ($_POST['YourArrow'])) {
            $possibleScores = array ("miss","1","3","5","7","9");
            $YourArrowLands = $_POST['YourArrow'];
            $_SESSION['YourArrowLands'] = $YourArrowLands;
            shuffle ($possibleScores);
            $ArrowLands = $possibleScores['0'];
            $ArrowLandsImage="0";
            if ($ArrowLands=="9"){
                $ArrowLandsImage="Gold.png";
            }elseif($ArrowLands=="7"){
                $ArrowLandsImage="Red.png";
            }elseif($ArrowLands=="5"){
                $ArrowLandsImage="Blue.png";
            }elseif($ArrowLands=="3"){
                $ArrowLandsImage="Black.png";
            }elseif($ArrowLands=="1"){
                $ArrowLandsImage="White.png";
            }elseif($ArrowLands=="miss"){
                $ArrowLandsImage="Miss.png";
            }
            echo "<p>Arrowlandsimage is ".$ArrowLandsImage."</p>\n";
            $_SESSION['ArrowImage'] = $ArrowLandsImage;
            echo $_SESSION['ArrowImage'];
            if ($_SESSION['YourArrowLands'] == $ArrowLands){
                $_SESSION['message'] = "Great job, you guessed right, you scored ".$ArrowLands.".\n";
            }else{
                $_SESSION['message'] = "Sorry, you guessed $YourArrowLands, but you scored ".$ArrowLands.".\n";
            }
    
            session_write_close();
            header("Location: 998.php");
            exit;
            return;
        }
    };
    function flashMessages(){
        if (isset($_SESSION['ArrowImage'])) {
            echo $_SESSION['ArrowImage'];
        }
        if ( isset ($_SESSION['message']) ) {
            echo("<p>".$_SESSION['message']."</p>\n");
        }
        if ( isset ($_SESSION['ArrowImage']) ) {
            echo '<img src="'.$_SESSION['ArrowImage'].'" title="Arrow Lands" alt="Arrow Lands" />';         
            echo "\n";
        }
    }
    
    GuessArrowGame();
    
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
          <meta charset="UTF-8">
          <title>Evolution Archers</title>
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body id="top">
        <div class=MainPage>
        <section>
            <h1>Can you hit the Gold?</h1>
    
            <form method="post">
            <p>
            <label for="GuessArrow">Where will your arrow land?</label>
            <select name="YourArrow" id="GuessArrow">
            <option value="9">9 - Gold</option>
            <option value="7">7 - Red</option>
            <option value="5">5 - Blue</option>
            <option value="3">3 - Black</option>
            <option value="1">1 - WhiteGold</option>
            <option value="miss">m - Miss</option>
            </select>
            </p>
            <input type="submit"/>
            </form>
    
            <?php flashmessages(); ?>
    
            </section>
        <footer>
               <p>All content &copy; Evolution Archers 2004-2018 or individual members of Evolution Archers.</p>
               <p>Evolution Archers are not responsible for the content of other websites.</p>
               <p>Links to other websites do not indicate any association between Evolution Archers and other websites.</p>
        </footer>
        </div>
    </body>
    </html>
    <?php
        unset($_SESSION['ArrowImage']);
        unset($_SESSION['message']);
        unset($_SESSION['success']);
    ?>