1

以下实际上是我的 php 类的作业。我在我的本地主机上解决了这个问题。一切都在那里运行完美,但是当我将其上传到我的实时站点时,逻辑变得一团糟。这是典型的“猜谜游戏”程序。没有报告错误。

唯一的问题是,在实时服务器上,用户可以输入任何内容 ——甚至是“1”,这表明他们的输入“太高了”。

这可能是我网站的(fatcow)php 设置的问题吗?我认为正在发生的事情不知何故将我的会话变量显示为“零”,而不是 rand(1, 10) 数字。为什么会发生这种情况?如何修复它以使其与在本地主机上的工作方式相同?

编码:

<?php

session_start();

error_reporting (E_ALL | E_STRICT);

$pageTitle = "Guessing Game";
include('includes/header.inc.php');

if (isset($_POST['finish']))
{
        unset($_SESSION);
        $_SESSION = array();
        session_destroy();
        die ('<h2>Congratulations</h2>
              <p>Your session has been reset.<br />
              Click <a href="guessingGame.php">HERE</a> to play again!</p>');
}
elseif (isset($_POST['guess']))
{
    $_SESSION['count'] = $_SESSION['count'] + 1;
    $guess = $_POST['guess'];
    if (!ctype_digit($guess) || $guess <= 0 || $guess > 10 )
    {
        echo'<p span class="italic">SORRY, that entry is invalid</span></p>';
    }
    else 
    {
        if ($guess == $_SESSION['num'])
        {
            die ('<h1>Hooray!</h1>
            <h2>You got it correct<br />
                And it only took you ' . $_SESSION['count'] . ' tries!</h2>
            <p>Click the button to finish:</p>
                <form action="guessingGame.php" method="post">
                  <input type="submit" name="submit" value="Finished" />
                  <input type="hidden" name="finish" value="true" />
                </form></p>');
        }
        elseif ($guess < $_SESSION['num']) 
        {
            echo '<h2>Sorry, that is incorrect.</h2>
                <p>Your guess is too LOW</p>';
        }
        else 
        {
            echo '<h2>Sorry, that is incorrect.</h2>
                <p>Your guess is too HIGH</p>';
        }               
    }
}
else
{
    $_SESSION['num'] = rand(1,10);
    $_SESSION['count'] = 0;
}
?>      
<h1>Please guess a number between 1 and 10</h1>
<p>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
        <input type="text" name="guess" size="2" />
        <input type="submit" name="submit" value="Submit" />
    </form>
</p>
<?php
include('includes/footer.inc.php');
?>
4

1 回答 1

0

(我将添加我的评论作为答案,因为它有助于解决问题。)

显然,Fat Cow 托管无法开箱即用地进行会话。一些链接可以看到:

B. 布尔佩特。我建议您尝试找到使用寄存器全局变量的解决方案,因为它已被弃用,将从 PHP 5.4.0 开始删除。

于 2013-04-20T21:12:41.787 回答