user3423927
user3423927

Reputation: 61

PHP Keep the variable scope even after the page reload

The website generates the random number from 1 to 100 when accessing the first page(page1.php). And the user will guess the number.

The first page contains - a text box for accepting a number from the user, and a submit button.

The second page(page2.php) will be returned to the user if the guess number is too high or too low. And the page shows a message telling the user "Too High" or "Too Low". The page also contains a button(retry button) that allows the user to go back to the first page(page1.php) to re-enter a new number a button that allows the user to quit the game.

The third page(page3.php) is returned to the user if the guess is correct. The page displays "Correct", the random number, and the count of tries.

And I have this index.php which is heart for all the pages. And here is the code.

index.php

    <?php

$name = '';
$inputnumber = '';
$random = 33; //this is just an assumption to keep it simple
$message = '';
$guesscount = '';
if (isset($_POST['action'])) {
    $action = $_POST['action'];
}

if ($action === 'guess') {
    $guesscount = $_POST['$guesscount'];
    $inputnumber = $_POST['$inputnumber'];
    if ($inputnumber == $random) {
        $message = "Correct!";
        include 'page3.php';
    }
    if ($inputnumber > $random) {
        $message = "Too High";
        include 'page2.php';
    }
    if ($inputnumber < $random) {
        $message = "Too Low";
        include 'page2.php';
    }
}

if ($action === 'retry') {
    include 'page1.php';
}

page1.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Number Guess</title>
    </head>
    <body>
        <h1>Number Guess</h1>
        <form name="myForm" action="index.php" method="post" >
            Number Guess: <input type="text" name="$inputnumber" value="<?php if(isset($inputnumber)==1){
echo $inputnumber;}else echo ""; ?>" /><br>

            <input type="submit" name="action" value="guess" />
            <hr>
            Guess Count: <?php echo $guesscount; ?>
        </form>
    </body>
</html>

page2.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Number Guess</title>
    </head>
    <body>
        <h1>Number Guess</h1>
        <form name="myForm" action="index.php" method="post" >
            Message: <?php echo $message; ?>
            <input type="hidden" name="$guesscount" value="<?php echo $guesscount;?>"/><br>
           <input type="submit" name="action" value="retry" />

            <hr>
           Guess Count: <?php echo $guesscount;?>
        </form>
    </body>
</html>

page3.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Number Guess</title>
    </head>
    <body>
        <h1>Number Guess</h1>
        <form name="myForm" action="index.php" method="post" >
            Message: <?php echo $message; ?>
            Number of Tries: <?php echo $guesscount; ?>
           <input type="submit" name="action" value="ok" />

        </form>
    </body>
</html>

page1.php is the page to load first.

Challenge I have faced is, I couldn't keep the $guesscount stable always. It keeps resetting on me. I have tried session but couldn't resolve it.Please help resolving it. Thanks in advance.

Upvotes: 0

Views: 1353

Answers (1)

Maciej Cygan
Maciej Cygan

Reputation: 5471

I don't know why but my gut feeling tells me that the reason why the session is not working for you on other pages is because you do not initiate it ??

So what you have to do is:

index.php

<?php
session_start();
$_SESSION['myVariable'] = 'myVariable';
?>

page1.php

<?php
session_start();
$mySessionVar = $_SESSION['myVariable'];
var_dump($mySessionVar); // <- this should print myVariable
?>

You may get an error saying that $_SESSION is null or not set and to prevent that you can just enclose $_SESSION inside and isset method

if(isset($_SESSION['myVariable']) && $_SESSION['myVariable'] != null) {
  $mySessionVar = $_SESSION['myVariable'[;
}

Upvotes: 1

Related Questions