Jump to content

Guessing game PHP with sessions


helpmehelpmehelpme

Recommended Posts

I am supposed to create a guessing game in PHP that uses sessions to store a random number between 0 and 100, along with the number of guesses the user has attempted. Each time the user guesses wrong, display the number of times the user has guessed. And a give up link that displays the generated number for the current game.  I have been working on this for a week and I can't figure it out.  Any help would be appreciated.

Here's my code thus far:

game.php

<?php
session_start();
if (!isset($_SESSION["randNum"])) {

    $_SESSION["randNum"]=rand(1,100);
}
$randNum=intval($_SESSION["randNum"]);
?>

<html>
<body>
<center><form action="game_check.php" method="POST">
Guess a number 1-100:<input type="text" name="userGuess"/>
<input type="submit" value="Guess"/>

</form></center>
<center><form action="game.php">
<input type="submit" value="Restart"/>
</form></center>
</body>
</html>

game_check.php

<?php
session_start();
$_SESSION["randNum"] = $_POST["randNum"];
$userGuess=$_POST["userGuess"];

if (isset($randNum)) {
    if ($userGuess<$randNum) {
            echo "<center>You guessed too low!</center>";
        }
    if ($userGuess>$randNum) {
            echo "<center>You guessed too high!</center>";
        }
    if ($userGuess==$randNum) {
            echo "<center>Congratulations You're right!!!</center>";
                         unset($_SESSION["randNum"]);
    }
}
else {
    echo "Uh oh";
}


?>

Link to comment
https://forums.phpfreaks.com/topic/252785-guessing-game-php-with-sessions/
Share on other sites

Try this:

 

game.php

<?php
session_start();

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

?>

<html>
<body>
<center><form action="game_check.php" method="POST">
Guess a number 1-100:<input type="text" name="userGuess"/>
<input type="submit" value="Guess"/>

</form></center>
<center><form action="game.php">
<input type="submit" value="Restart"/>
</form></center>
</body>
</html>

game_check.php

<?php
session_start();

$randNum = $_SESSION['randNum'];
$userGuess=$_POST["userGuess"];

if (isset($randNum)) {
    if ($userGuess<$randNum) {
            echo "<center>You guessed too low!</center>";
            $_SESSION['randNum']++;
        }
    if ($userGuess>$randNum) {
            echo "<center>You guessed too high!</center>";
            $_SESSION['randNum']++;
        }
    if ($userGuess==$randNum) {
            echo "<center>Congratulations You're right!!!</center>";
                         unset($_SESSION["randNum"], $_SESSION['guesses']);
    }
}
else {
    echo "Uh oh";
}


?>

I added it, but I made a typo - so here it is:

 if ($userGuess<$randNum) {
            echo "<center>You guessed too low!</center>";
            $_SESSION['guesses']++;
        }
    if ($userGuess>$randNum) {
            echo "<center>You guessed too high!</center>";
            $_SESSION['guesses']++;
        }
    if ($userGuess==$randNum) {
            echo "<center>Congratulations You're right!!!</center>";
                         unset($_SESSION["randNum"], $_SESSION['guesses']);
    }

 

Note the "$_SESSION['guesses']++" lines. Every time they make a wrong guess, the guesses is increment.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.