PHP放弃链接和显示编号链接

时间:2016-02-14 23:25:20

标签: php

为此,我被困在两个部分。我需要一个放弃链接,显示当前游戏生成的数字。还包括一个“重新开始”链接,用于删除用户会话并使用标题(“location:URL”)功能导航到主页面。 这是我到目前为止的游戏代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Guessing Game</title>
</head>
<body>
<center><form "action="game.php" method="POST">
Guess a number 1-100:<input type="text" name="userGuess"/>
<input type="submit" value="Guess"/>
</form>
</body>
</html>

<?php
session_start();

$_SESSION['randNum'] = isset($_SESSION['randNum']) ? $_SESSION['randNum'] : rand(0, 100);
$_SESSION['guesses'] = isset($_SESSION['guesses']) ? $_SESSION['guesses'] : 0;

$randNum = $_SESSION['randNum'];
$userGuess = filter_input(INPUT_POST, "userGuess");


if (isset($randNum)) {
    if ($userGuess<$randNum) {
            echo "<center>You guessed too low!</center>";
            $_SESSION['guesses']++;
            echo $_SESSION['guesses'];
        }
    if ($userGuess>$randNum) {
            echo "<center>You guessed too high!</center>";
            $_SESSION['guesses']++;
            echo $_SESSION['guesses'];
        }
    if ($userGuess==$randNum) {
            echo "<center>Congratulations You're right!!!</center>";
                         unset($_SESSION["randNum"], $_SESSION['guesses']);
    }
    if ($userGuess>100 || $userGuess<0) {
            echo "<center>Please enter a number betweeen 1 and 100!</center>";
    }
}
else {
    echo "Please enter a number between 1 and 100";
}
?>

2 个答案:

答案 0 :(得分:0)

有一些调整,但基本上只是在表单上添加了几个按钮,在服务器端添加了一些字段处理:

<?php
session_start();

function resetGame() {
    unset($_SESSION["randNum"], $_SESSION['guesses']);
}
function restart() {
    header("location: game.php");
}

$_SESSION['randNum'] = isset($_SESSION['randNum']) ? $_SESSION['randNum'] : rand(0, 100);
$_SESSION['guesses'] = isset($_SESSION['guesses']) ? $_SESSION['guesses'] : 0;
$giveUp = isset($_POST['give-up']);
$solution = isset($_POST['solution']);

$randNum = $_SESSION['randNum'];
$userGuess = filter_input(INPUT_POST, "userGuess");

if ($giveUp) {
    resetGame();
    restart();
} else if ($solution) {
   echo "The solution was ". $randNum ."!!";
   resetGame();
} else if (isset($randNum) && !is_null($userGuess)) {
    if ($userGuess<$randNum) {
        echo "<center>You guessed too low!</center>";
        $_SESSION['guesses']++;
        echo $_SESSION['guesses'];
    }      
    if ($userGuess>$randNum) {
        echo "<center>You guessed too high!</center>";
        $_SESSION['guesses']++;
        echo $_SESSION['guesses'];
    }
    if ($userGuess==$randNum) {
        echo "<center>Congratulations You're right!!!</center>";
        resetGame();
    }
    if ($userGuess>100 || $userGuess<0) {
        echo "<center>Please enter a number betweeen 1 and 100!</center>";
    }
} else {
    echo "Please enter a number between 1 and 100";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Guessing Game</title>
</head>
<body>
<center><form "action="game.php" method="POST">
Guess a number 1-100:<input type="text" name="userGuess"/>
<input type="submit" value="Guess"/>
<input type="submit" id="give-up" name="give-up" value="Give up"/>
<input type="submit" id="solution" name="solution" value="Show Solution"/>
</form>
</body>
</html>

答案 1 :(得分:0)

这是带有一些调整/更正的代码:

  1. <form "action="game.php"><form action="game.php">
  2. 已关闭<center>代码。
  3. 由于存在标题功能,PHP必须先发送/打印出任何内容。
  4. 消息应显示在<body>标记中。
  5. $userGuess的截止值应小于1,而不是0
  6. 更改了if/else语句的结构以减少混乱。
  7. <input type="text">更改为<input type="number">

    <?php
    
    session_start();
    
    $_SESSION['randNum'] = isset($_SESSION['randNum']) ? $_SESSION['randNum'] : rand(0, 100);
    $_SESSION['guesses'] = isset($_SESSION['guesses']) ? $_SESSION['guesses'] : 0;
    
    $randNum = $_SESSION['randNum'];
    
    if (isset($_POST['userGuess'])) {
    
        $userGuess = $_POST['userGuess'];
    
        if ($userGuess > 100 || $userGuess < 1) {
    
            $message = "Please enter a number betweeen 1 and 100!";
    
        } else {
    
            if ($userGuess < $randNum) {
    
                    $message = "You guessed too low!";
                    $count = $_SESSION['guesses'];
                    $_SESSION['guesses']++;
    
                } elseif ($userGuess>$randNum) {
    
                    $message = "You guessed too high!";
                    $count = $_SESSION['guesses'];
                    $_SESSION['guesses']++;
    
                } else {
    
                    $message = "Congratulations You're right!!!";
                    session_unset();
    
            }
    
        }
    
    }
    
    if (isset($_GET['giveup'])) {
    
        $message = "The answer was " . $randNum . ". I picked a new number to play again.";
        $_SESSION['randNum'] = rand(0, 100);
        $_SESSION['guesses'] = 0;
    
    } else if (isset($_GET['restart'])) {
    
        session_unset();
        header("location: game.php");
        exit();
    
    }
    
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Guessing Game</title>
    </head>
    
    <body>
    <center>
    
    <?php
    
    echo (isset($message) ? ($message . '<br />') : false) . (isset($count) ? ($count . '<br />') : false);
    
    ?>
    
    <form action="game.php" method="POST">
    Guess a number 1-100:
    <input type="number" name="userGuess" />
    <input type="submit" value="Guess" />
    </form>
    
    <a href="game.php?giveup">Give up</a>
    <a href="game.php?restart">Start over</a>
    
    </center>
    </body>
    
    </html>