mercredi 27 mai 2020

PHP Random number not staying the same in a session

I'm trying to make a site where it creates a random number, then stores it for the whole time that the tab is open. It all seems to work, except the number changes with every guess. I haven't used sessions very much so it may be obvious what is wrong. I made the code output the random number so I can check if it's changing. Ideally, the random number stays the same until they guess correctly, then they hit the retry button the reload the site. Thanks !

<?php
  if(session_status() === PHP_SESSION_NONE){
  session_start();                                      #starts a session if none are started
  }

      if(!isset($_SESSION['randomNum'])) {              #creates number 1-100
      $_SESSION['randomNum'] = rand(1,100);
    }
      $randomNum=intval($_SESSION['randomNum']);        #sets input to variable of $randomNum
      if(!isset($_SESSION['counter'])) {                #starts the counter and sets it to 1
        $_SESSION['counter'] = 1;
      }
      $output_ct = $_SESSION['counter'];

      if(isset($_GET['guess'])){                        #if its inputted, its get the guess
      $guess = $_GET['guess'];                          #the guess is assigned to $guess

      if($guess < $randomNum){                          #if the guess is lower than the randomNum
        $output = "Guess of $guess is too low";
        ++$_SESSION['counter'];                         #adds 1 to counter
      }
      elseif($guess > $randomNum){                      #if the guess is higher then the randomNum
        $output = "Guess of $guess is too high";
        ++$_SESSION['counter'];                         #adds 1 to counter
      }

      else{                                             #if the guess is equal to the randomNum
        $output_ct = "You've had . $output_ct . guesses";
        "<br>";
        $output = "Congratulations! Your guess of $guess was correct.";
        "<br>";
        $n_guesses = "You guessed correctly in $output_ct tries.";

        session_destroy();
      }
    }
 ?>

 <!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <title>
        FOLIO 4
    </title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link href="stylesheet.css" type="text/css"rel="stylesheet">
  </head>

  <body>
    <div class="sidenav">
        <a href="index.php">Home</a>
        <a href="folio1.php">Folio 1</a>
        <a href="folio2.php">Folio 2</a>
        <a href="folio3.php">Folio 3</a>
        <a href="folio4.php">Folio 4</a>
        <a href="folio5.php">Folio 5</a>
    </div>

    <div class="main">
      <center>
        <h1>Folio 4</h1>
          <br>
              <h5>The user inputs a number between 1 and 100. The site will
                  tell them if their guess was too high or too low, and allows
                  them to guess again. When the user gets the number correct, the
                  site will congratulate them and tell the user how many guesses
                  they took.
              </h5>
          <br>

            <div class="form" action="folio4.php">
                <form method="get">
                  <label for="guess">Guess a number between 1 and 100 &nbsp</label>
                  <input type="number" name="guess" id="guess" min="1" max="100" required>

                    <br><br><br>

                  <button type= "submit">Submit</button>
                </form>

            <br><br>
            </div>

            <div class="output">
              <p>
              <?php
                echo $output;
                "<br>";
                echo $n_guesses;
                "<br>";
                echo $_SESSION['randomNum'];
               ?>
              </p>

            </div>

              <br>

            <div class="retry">
              <a href=folio4.php>Play again</a>

                <?php
                  session_unset();
                  session_destroy();
                 ?>

            <br><br><br>
            </div>

           <a href="">
           See planning for Folio 4
           </a>

    </div>
  </body>
</html>




Aucun commentaire:

Enregistrer un commentaire