-1

I am attempting to make a random number guessing game in PHP. The guesses are entered into the $_GET var in the address bar after a ?. However, each time the enter key is pressed the random number itself changes. How can I keep this random number intact until the game is lost or won.

<!DOCTYPE html>
<html>
<head>
    <title>Guess Gaming</title>
</head>
<body>
    <h1>Guess the correct Number</h1>
    <p>
    <?php
      $randomNum = rand(0, 150);
      if(empty($_GET["guess"])){
          echo "Missing guess parameter.";

      } elseif(!empty($_GET["guess"])){

    if(is_numeric($_GET["guess"])){
        if($_GET["guess"] === $randomNum){
            echo "Congratulations-- You are right";
        } elseif($_GET["guess"] < $randomNum){
            echo "Your guess is too low.";
        } elseif($_GET["guess"] > $randomNum){
            echo "Your guess is too high.";
        }

    } else {
        echo $_GET["guess"]." isn't numeric.";
    }
}   
?>
</p>
<p>  
<?php
   echo $randomNum;
?>
</p>    
</body>
</html>
7
  • Does this game include money? in other words does it have to be secure from fraud?
    – Spoody
    Jan 15, 2018 at 14:42
  • 1
    @MehdiBounya Would you bet money on this? :-)
    – Andreas
    Jan 15, 2018 at 14:43
  • For how long should this random number be keept? Should user 2 have the same number to guess on or should it get a new number?
    – Andreas
    Jan 15, 2018 at 14:44
  • No money. Just learning PHP. Jan 15, 2018 at 14:45
  • 1
    Save $randomNum into session, then just use that session when checking. When he will be right, reset that session with new number. Jan 15, 2018 at 14:46

2 Answers 2

3

Use sessions. Put session_start(); at the top of your PHP script.

Generate a new random number whenever the current one is guessed, or if there is none yet:

if (!isset($_SESSION['secret_random_number']))
{
  $_SESSION['secret_random_number'] = mt_rand(0,150);
}

Get the previously stored number like this:

$randomNumber = $_SESSION['secret_random_number'];
7
  • 1
    Better answer. Just add: if($_GET["guess"] === $randomNum){ unset($_SESSION['secret_random_number']);} also.
    – Andreas
    Jan 15, 2018 at 14:58
  • 1
    No that's not a better answer because more has to be done when the guess is correct.
    – jhilgeman
    Jan 15, 2018 at 15:08
  • Sad. This is not the best answer. This is feeding a man a fish for a day. Stop being afraid of the official documentation.
    – jhilgeman
    Jan 15, 2018 at 15:11
  • @jhilgeman It is a better answer. Yours is not an answer. It's a link. Thats it. It should be posted as a comment, not an answer, I can't understand why you even got upvoted at all. This answer however is a good attempt to help the asker.
    – Andreas
    Jan 15, 2018 at 15:11
  • This answer isn't helping the asker in the long run. He said he's learning PHP and you're trying to give him a quick answer instead of pointing him to a resource that not only has the same code answer but has additional information to help the asker with future questions.
    – jhilgeman
    Jan 15, 2018 at 15:14
1

Store the correct answer in a session variable. Session variable persist from page to page and aren't visible to the end user. http://php.net/manual/en/session.examples.basic.php

EDIT: Adding the code example from the documentation, since some people are otherwise voting in retaliation to delete a completely valid answer simply because it doesn't have code.

<?php
session_start();
if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
} else {
  $_SESSION['count']++;
}
?>

That is the code principle. Just change out the variable names and use your existing logic for generating the random number.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.