Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function start_again(){
- $_SESSION['num'] = $number = rand(1, 50);
- $_SESSION['attempt'] = 0;
- echo '<p>Guess new number!</p>';
- }
- if (!isset($_SESSION)){
- session_start();
- }
- var_export($_SESSION); echo '<br/>';
- ?><!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Number Guessing Game!</title>
- </head>
- <body>
- <?php
- $userInput = $_POST['number'];
- if(!isset($_SESSION['num'])){
- start_again();
- }
- else{
- $number = $_SESSION['num'];
- $attempt = $_SESSION['attempt'];
- if ($number==$userInput) { $message = 'Bingo!'; }
- if ($number<$userInput) { $message = 'random number is smaller!'; $_SESSION['attempt']++; }
- if ($number>$userInput) { $message = 'random number is bigger!'; $_SESSION['attempt']++; }
- echo "you entered $userInput <br/>$message <br>";
- }
- echo "<h3> $number / $attempt </h3>"; //just to see if the number is changing everytime
- if ($number==$userInput) { start_again(); }
- ?>
- <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
- <input type="text" name="number" value="" /> <input type="submit" name="Submit"
- value="Submit" />
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement