-1

How to not change the value of the random number, if I did not choose the correct number?

After correct num, the program should change the value of random num.

Without using cookies and session!

Here is PHP code for Guess Number Game

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="game.php" method="get">
        <input type="text" name="number" placeholder="50-100">
        <br><br>
        <input type="submit" name="submit" value="submit">
        <input type="submit" name="other" value="reset number">
    </form>

    <?php

    $x   = rand(50, 100);
    $num = '';

    if (isset($_GET['submit'])) {
     $num = $_GET['number']; // Add this to set value for $num variable
            if ($num < $x) 
            {
                echo " Your number is lower! <br />";
            } elseif ($num > $x) 
                {
                echo " Your number is higher! <br />";
            } elseif ($num == $x) 
                {
                echo " Congratulations! You guessed the hidden number. <br />";
            } else 
                {
                echo " You never set a number! <br />";
            }
    }
    ?>
</body>
</html>
1
  • You either need to store it somewhere, where it can be read from again on the next request (like a session), or you need to send it together with the form, as a hidden field. (Yes, in that case it will show up in the address bar, unless you switch the form submission method to POST. And even then it would still be discoverable by the user, if they checked the actual source code/DOM view in their browser.)
    – CBroe
    Apr 22, 2020 at 11:35

2 Answers 2

0

I'd suggest you save your variable in $_SESSION[], so you get something like this:

if (isset($_SESSION['x'])) {
  $x   = $_SESSION['x'];
}
else {
  $x   = rand(50, 100);
}

Otherwise you may store it in a hidden field:

    <input type="hidden" name="x" value="<?php $x ?>">

then you read it like this:

if (isset($_GET['x'])) {
  $x   = $_GET['x'];
}
else {
  $x   = rand(50, 100);
}
0

You can use PHP session handling.

<?php

session_start();

if (empty($_SESSION['value']) || 'reset number' === $_GET['other'] ?? '') {
    $_SESSION['value'] = rand(50, 100);
}
$x = $_SESSION['value'];

$num = '';

if (isset($_GET['submit'])) {
    $num = $_GET['number']; // Add this to set value for $num variable
    if ($num < $x) {
        echo " Your number is lower! <br />";
    } elseif ($num > $x) {
        echo " Your number is higher! <br />";
    } elseif ($num == $x) {
        echo " Congratulations! You guessed the hidden number. <br />";
    } else {
        echo " You never set a number! <br />";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <form action="game.php" method="get">
            <input type="text" name="number" placeholder="50-100">
            <br><br>
            <input type="submit" name="submit" value="submit">
            <input type="submit" name="other" value="reset number">
        </form>
    </body>
</html>

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