How can I compare session in PHP

I want to have a random number between 1-100 and stores the number in session. I also want to have an input box where a user can guess a number from 1-100 and compare their number to the random number generated and tell the user if it’s high or low from the number being compared

How can I also reset button where a new random number will be generated and the user will have to guess it again.

What have you tried? Do you have some code you can share?

1 Like





$_SESSION["correct_answer"] = rand(1,10);

$_SESSION["guess"] = 0;


<!DOCTYPE html>

<html lang="en">


    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">




    <h1>Welcome to the Great Number Game!</h1>

    <h2>I am thinking of a number between 1 and 100</h2>

    <h2>Take a guess!</h2>

    <form method="post" action="tryguess.php">

        <input type="text" id="guess" name="guess">

        <input type="submit" value="submit">

        <!-- <a href="trynumber.php"><input type="submit" value="reset"></a> -->







// header('Location: trynumber.php');

if (isset($_POST['guess'])) {

    if ($_POST['guess'] == $_SESSION['correct_answer']) {

        echo "correct!";


    if ($_POST['guess'] < $_SESSION['correct_answer']) {

        echo "too low";


    if ($_POST['guess'] > $_SESSION['correct_answer']) {

        echo "too high";




I am trying 10 numbers first

Ok… that seems fine.

If you want to have a reset button you could simple put something like this in your tryguess.php file (after closing php):

<button onclick="window.location.href='trynumber.php'">Reset</button>

That was what I am thinking but our lesson is about header(location)

I see… well then your button could go to another php which reads:


header('Location: trynumber.php');
Sponsor our Newsletter | Privacy Policy | Terms of Service