php 糖豆游戏 - 顺序不正确

php jelly bean game - incorrect sequencing

谁能帮忙,我的游戏顺序不正确,我到处都有回声线 x,所以我可以看顺序。此时,它从猜测形式(第二个序列)跳到第一个形式(您设置游戏限制的地方)。

<?
//$stage = 1;
//$_SESSION['stage'] = 1;
//if(!$_SESSION['stage']){$_SESSION['stage'] = 1;}
if(!$_SESSION['stage']){$_SESSION['stage'] = $stage;}


/* sequence control
    $_SESSION['stage']++;
    $_SESSION['stage'] = 1;
    echo "stage at" . $_SESSION['stage'];
*/

if($_SESSION['stage'] == 1){
    if(!$_SESSION['r']){
            $_SESSION['stage'] = 1;
            firstForm();
            echo "<p>31</p>";
            //exit();
        }else{
        if($_POST['submit']){
            echo "<p>line 35</p>";
            if($_SESSION['stage'] == 1){
                echo "<p>line 37</p>";
                checkRange();
            }else{echo "<p>line 39</p>";$_SESSION['stage']++;}
        }
        if(!$_POST['submit']){
            echo "<p>42</p>";
            firstForm();
        }
    }
}
if($_SESSION['stage'] >= 2){
    echo "48";
    if(!$_SESSION['r']){
            echo "49 stage at" . $_SESSION['stage'];
            //guessForm();
        }
    if(!$_POST['guess'] && $_POST['submit'] != "guess"){
        echo "<p>line 54</p>";
        if($_SESSION['stage'] == 2){
            echo "<p>line 56</p>";
            guessForm();
        }else{
            echo "<p>line 59</p>";
            checkGuess($_POST['guess']);
        }
    }else{
        echo "<p>line 63</p>";
        checkGuess($_POST['guess']);
        if($_POST['guess']<$_SESSION['r']){
            echo "Guess was too low, try again.";
            guessForm();
        }elseif($_POST['guess']>$_SESSION['r']){
            echo "Guess was too high, try again.";
            guessForm();
        }else{
            echo "<h1>Correct Guess! Well done.</h1>";
            echo "<h2>It took you " . $_SESSION['counter'] . " guesses.";
            restartForm();
            $_SESSION['r'] = null;
            $_SESSION['counter'] = null;
            $_SESSION['stage'] = 1;
        }
        echo "<p>line 77</p>";
        $_SESSION['stage']=2;
        echo "<p>current Step: (".$_SESSION['stage'].")</p>";
    }
    echo "<p>line 83</p>";
    $_SESSION['stage']=2;
    echo "<p>current Step: (".$_SESSION['stage'].")</p>";
}

echo "<p>current Step: (".$_SESSION['stage'].")</p>";


function checkRange(){
        if(!$_POST['jellyBeans']){
            echo "<h2>You did not enter a capacity.</h2>";
            firstForm();
            exit();
        }elseif(!is_numeric($_POST['jellyBeans'])){
            echo "<h2>Numbers only.</h2>";
            firstForm();
            exit();
        }elseif($_POST['jellyBeans'] <= 0){
            echo "<h2>Try a bigger number.</h2>";
            firstForm();
            exit();
        }elseif($_POST['jellyBeans'] > 10000){
            echo "<h2>Slow down partner, choose somthing smaller</h2>";
            firstForm();
            exit();
        }else{
            echo "<p>107</p>";
            echo "<p>random number is: " . $_SESSION['r'] = rand(0,$_POST['jellyBeans']) . "</P>";
            $_SESSION['stage']++;
            echo "<p>current Step: (".$_SESSION['stage'].")</p>";
        }
    }

function firstForm(){
?>
<form acton="<?=$_SERVER['php_self']?>" method="post">
    <p><label for="jellyBeans">How many jelly beans do you want to be in the game</label><input name="jellyBeans"value="<?=$_POST['jellyBeans']?>" ></p>
    <p>
        <button value="submit" name="submit" type="submit">Choose</button>
    </p>
    <p>
        <button value="<? $_SESSION['stage'] = 1 ?>" name="submit" type="submit">Restart?</button>
    </p>
</form>
<? }

function guessForm(){
?>
<form acton="<?=$_SERVER['php_self']?>" method="post">
    <p><label for="guess">Guess what random number was selected</label><input name="guess" type="text" ></p>
    <p>
        <button value="submit" name="submit" type="submit">Guess</button>
    </p>
    <p>
        <button value="<? $_SESSION['stage'] = 1 ?>" name="submit" type="submit">Restart?</button>
    </p>
</form>
<? }

function checkGuess($val){
    echo var_dump($val);
    if($val == ""){
        $error[] = "You did not enter a value.";
    }else{
        if(!is_numeric($val)){
        $error[] = "The value you entered was not a number.";
        }
    }
    if($error){
        foreach($error as $v){
            echo "<h2>".$v."</h2>";
        }
        echo "<h3>Try again.</h3>";
        guessForm();
        exit();
    }
}

?>

快速浏览一下..

您需要添加一个 session_start(); 以便在页面加载之间记住您的 $_SESSION。

附带说明:请在第一行使用完整的 php 标签 <?php 而不是 <?,因为它并非总是在所有服务器上启用。

<?php
session_start(); // ** Added
// The Rest of your code //