Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Day 9 - Lets Make A Game - Part 2 - Become PHP Expert in 30 days

  1. #1
    Join Date
    Sep 2003
    Posts
    3,040

    Default Day 9 - Lets Make A Game - Part 2 - Become PHP Expert in 30 days

    Lets Make A Game - Part 2



    On Day 8, we made Number Guessing Game in PHP. If you have problem understanding Day 8 script, don't worry, it is slightly complicated script, you will understand it better as we keep improving the script.

    Lets start with previous lesson code.

    day_8_ex_2.php

    PHP Code:
    <html>
    <body>

    <h1>Number Guessing Game.</h1>

    <?php

    if (isset($_GET['secretNumber'])) {
        
    $secretNumber $_GET['secretNumber'];
    } else {
        
    $secretNumber rand(1,10);
    }

    if (isset(
    $_GET['userNumber'])) {
        
    $userNumber $_GET['userNumber'];

        if (
    $secretNumber $userNumber) {
            echo 
    '<h1 style="color:red">Your number is too SMALL.</h1>';
        } else if (
    $secretNumber $userNumber) {
            echo 
    '<h1 style="color:blue">Your number is too BIG.</h1>';
        } else {
            echo 
    '<h1 style="color:green">You win the game.</h1>';
        }
    }

    ?>

    <form method="GET" action="">
        Enter Number: <input name="userNumber" type="text">
        <button type="submit">Check</button>
        <input type="hidden" name="secretNumber" value="<?php echo $secretNumber?>">
    </form>

    <h2>How to Play:</h2>

    <pre>
    * Server will pick a secret number between 1 and 10.
    * You guess what number it is.
    * If your guess is too high or too low, Server will give you a hint.
    * See how many turns it takes you to win!
    </pre>

    </body>
    </html>
    The script have some flaws. As you can see in the picture below, our secret number is not secret.



    In this case, secretNumber is passed using GET method to server.

    Code:
    day_8_ex_2.php?userNumber=5&secretNumber=2
    Anyone looking at the URL can see what the secretNumber server have generated using rand() function and enter that number and WIN (or cheat) the game.

    What we can do to stop the secretNumber showing up in the URL ?

    Welcome to POST Method.

    Web servers support POST and GET methods.

    GET methods generally used for small data and when you need to allow users to use browser back/forward button. Data passed using GET method is limited. Most search engines use GET method, because that allow you to use back/forward feature in browser, for example

    Code:
    https://www.google.com/search?q=become+php+expert+in+30+days
    Who Use POST Method and Why

    Most banks use POST method for login page, if not anyone will be able to see your password and user name by looking at address bar of your browser.

    Any FORM page that send large data or sensitive data (for example password, credit card number, etc..) must use POST method.

    $_POST Associative Array in PHP

    Like $_GET array in PHP, you can use $_POST array to read data send to server with POST method.

    To change a FORM page to use POST method, replace GET with POST

    In our example script.

    Find

    Code:
    <form method="GET" action="">
    Replace With

    Code:
    <form method="POST" action="">

    Lets Update Our Game To Use Post Method

    Its easy, Find all GET and replace it with POST.

    day_9_ex_1.php


    PHP Code:
    <html>
    <body>

    <h1>Number Guessing Game.</h1>

    <?php

    if (isset($_POST['secretNumber'])) {
        
    $secretNumber $_POST['secretNumber'];
    } else {
        
    $secretNumber rand(1,100);
    }

    if (isset(
    $_POST['userNumber'])) {
        
    $userNumber $_POST['userNumber'];

        if (
    $secretNumber $userNumber) {
            echo 
    '<h1 style="color:red">Your number is too SMALL.</h1>';
        } else if (
    $secretNumber $userNumber) {
            echo 
    '<h1 style="color:blue">Your number is too BIG.</h1>';
        } else {
            echo 
    '<h1 style="color:green">You win the game.</h1>';
        }
    }

    ?>

    <form method="POST" action="">
        Enter Number: <input name="userNumber" type="text">
        <button type="submit">Check</button>
        <input type="hidden" name="secretNumber" value="<?php echo $secretNumber?>">
    </form>

    <h2>How to Play:</h2>

    <pre>
    * Server will pick a secret number between 1 and 100.
    * You guess what number it is.
    * If your guess is too high or too low, Server will give you a hint.
    * See how many turns it takes you to win!
    </pre>

    </body>
    </html>
    Upload the script to your web site and run

    Code:
    http://YOUR-SITE/day_9_ex_1.php
    When you submit the form, no data is passed through URL, so you can't just look at URL and find the secret number.

    Still there is one more way to CHEAT in this game, looking at source code of your HTML page and finding value of hidden field with name secretNumber. We will fix it on Day 10.




    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  2. #2
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

    http://php.flashwebhost.com/annie/9day_number_game.php
    Code:
    <html>
    <body>
    
    <h1>Guess the Secret Number - Numbers up to 1 to 20 </h1>
    
    <?php
    
    if (isset($_POST['secretNumber'])) {
    $secretNumber = $_POST['secretNumber'];
    } else {
    $secretNumber = rand(1,20);
    }
    if (isset($_POST['userNumber'])) {
    $userNumber = $_POST['userNumber'];
    if ($secretNumber > $userNumber) {
    echo '<h1 style ="color:Blue"> Your number is too Small >/h1>';
    } else if ($secretNumber < $userNumber) {
    echo '<h1 style ="color:Red"> Your number is too BIG <h1>';
    }  else {
    echo '<h1 style="color:green">You win the game.</h1>';
    }
    } 
    ?> 
    
    <form method = "POST" action="">
    Enter Number: <input name="userNumber" type="text">
    <button type = "submit"> Check</button>
    <input type="hidden" name="secretNumber" value"<?php echo $secretNumber; ?>">
    </form>

  3. #3
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/9...essinggame.php

    Code:
    <html>
    <body>
    
    <h1>Guessing Game</h1>
    
    <?php
    
    if (isset($_POST['guessNumber'])) {
        $guessNumber = $_POST['guessNumber'];
    } else {
        $guessNumber = rand(1,50);
    }
    
    if (isset($_POST['userNumber'])) {
        $userNumber = $_POST['userNumber'];
    if ($guessNumber > $userNumber) {
            echo '<h1 style="color:green">Your number is too SMALL.</h1>';
        } else if ($guessNumber < $userNumber) {
            echo '<h1 style="color:brown">Your number is too BIG.</h1>';
        } else {
            echo '<h1 style="color:orange">You win the game.</h1>';
        }
    }
    
    ?>
    
    <form method="POST" action="">
    Enter Number: <input name="userNumber" type="text">
    <button type="submit">Check</button>
    <input type="hidden" name="guessNumber" value="<?php echo $guessNumber; ?>">
    </form>
    
    <h2>How to Play:</h2>
    
    <pre>
    * Server will pick a guess number between 1 and 50.
    * You guess what number it is.
    * If your guess is too high or too low, Server will give you a hint.
    * See how many turns it takes you to win!
    </pre>
    
    </body>
    </html>

  4. #4
    Join Date
    Feb 2005
    Location
    India
    Posts
    11,004

    Default

    http://php.flashwebhost.com/melbin/number_game_post.php

    PHP Code:
    <html>
    <body>

    <h1>Guess the Number</h1>

    <?php

    if (isset($_POST['secretNumber'])) {
        
    $secretNumber $_POST['secretNumber'];
    } else {
        
    $secretNumber rand(1,100);
    }

    if (isset(
    $_POST['userNumber'])) {
        
    $userNumber $_POST['userNumber'];

        if (
    $secretNumber == $userNumber) {
            echo 
    '<h1 style="color:green">You are right...!!!!</h1>';
            echo 
    '<h1 style="color:green">Secret Number is ' $secretNumber '</h1><br>';

        } else {
            echo 
    '<h1 style="color:red">You are Wrong</h1>';
            echo 
    '<h1 style="color:#FFF">Secret Number was ' $secretNumber '</h1><br>';
            echo 
    '<h3 style="color:blue">Press Ctrl + A to see the secret number</h3><br>';
        }
    }

    ?>

    <form method="POST" action="">
        Enter Number: <input name="userNumber" type="text">
        <button type="submit">Check</button>
        <input type="hidden" name="secretNumber" value="<?php echo $secretNumber?>">
    </form>
    VIDEO WORLD : LATEST HOLLYWOOD || BOLLYWOOD || SOUTH INDIAN VIDEOS || TRAILERS

  5. #5
    Join Date
    Nov 2004
    Location
    India
    Posts
    65

    Default

    http://php.flashwebhost.com/stefin/games_10.php

    Code:
    <html>
    <body>
    <h1>Number Guessing Game.</h1>
    
    <?php
    
    if (isset($_POST['secretNumber'])) {
    $secretNumber = $_POST['secretNumber'];
    } else {
     $secretNumber = rand(1,500);
    }
    if (isset($_POST['userNumber'])) {
    $userNumber = $_POST['userNumber'];
    if ($secretNumber > $userNumber) {
    echo '<h1 style="color:BLACK">Think Greater.</h1>';
    } else if ($secretNumber < $userNumber) {
    echo '<h1 style="color:GREEN">Think Smaller.</h1>';
    } else {
    echo '<h1 style="color:RED">Good Job, You win the game.</h1>';
    }
    }
    ?>
    
    <form method="POST" action="">
    Enter Number: <input name="userNumber" type="text">
    <button type="submit">Check</button>
    <input type="hidden" name="secretNumber" value="<?php echo $secretNumber; ?>">
    </form>

  6. #6
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by stefin View Post
    http://php.flashwebhost.com/stefin/games_10.php

    Code:
    <html>
    <body>
    <h1>Number Guessing Game.</h1>
    
    <?php
    
    if (isset($_POST['secretNumber'])) {
    $secretNumber = $_POST['secretNumber'];
    } else {
     $secretNumber = rand(1,500);
    }
    if (isset($_POST['userNumber'])) {
    $userNumber = $_POST['userNumber'];
    if ($secretNumber > $userNumber) {
    echo '<h1 style="color:BLACK">Think Greater.</h1>';
    } else if ($secretNumber < $userNumber) {
    echo '<h1 style="color:GREEN">Think Smaller.</h1>';
    } else {
    echo '<h1 style="color:RED">Good Job, You win the game.</h1>';
    }
    }
    ?>
    
    <form method="POST" action="">
    Enter Number: <input name="userNumber" type="text">
    <button type="submit">Check</button>
    <input type="hidden" name="secretNumber" value="<?php echo $secretNumber; ?>">
    </form>
    Good script.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  7. #7
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by annie View Post
    http://php.flashwebhost.com/annie/9day_number_game.php
    Code:
    <html>
    <body>
    
    <h1>Guess the Secret Number - Numbers up to 1 to 20 </h1>
    
    <?php
    
    if (isset($_POST['secretNumber'])) {
    $secretNumber = $_POST['secretNumber'];
    } else {
    $secretNumber = rand(1,20);
    }
    if (isset($_POST['userNumber'])) {
    $userNumber = $_POST['userNumber'];
    if ($secretNumber > $userNumber) {
    echo '<h1 style ="color:Blue"> Your number is too Small >/h1>';
    } else if ($secretNumber < $userNumber) {
    echo '<h1 style ="color:Red"> Your number is too BIG <h1>';
    }  else {
    echo '<h1 style="color:green">You win the game.</h1>';
    }
    } 
    ?> 
    
    <form method = "POST" action="">
    Enter Number: <input name="userNumber" type="text">
    <button type = "submit"> Check</button>
    <input type="hidden" name="secretNumber" value"<?php echo $secretNumber; ?>">
    </form>
    This script have bugs.

    bug 1

    Code:
    echo '<h1 style ="color:Blue"> Your number is too Small >/h1>';
    >/h1> instead of </h1>

    bug 2

    Code:
    echo '<h1 style ="color:Red"> Your number is too BIG <h1>';
    <h1> instead of </h1>
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  8. #8
    Join Date
    May 2014
    Posts
    21

    Default

    Code:
     <input type="hidden" name="secretNumber" value="<?php echo $secretNumber; ?>">
    In this code whats
    Code:
    value="<?php echo $secretNumber; ?>
    does?

  9. #9
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by Austinwhite View Post
    Code:
     <input type="hidden" name="secretNumber" value="<?php echo $secretNumber; ?>">
    In this code whats
    Code:
    value="<?php echo $secretNumber; ?>
    does?
    In this game, you need to guess secretNumber. This number do not change on each request to the server. It only generated once, on your first visit to the game page, this number is generated.

    When you submit form with your guessed number, how server know what was the secretNumber it generated on previous request ?

    Web server do not have memory, it is like an alzheimer patient, so you have to tell it the secretNumber it generated on each request, if not it will think it did not generated one and generate a new number.

    In out case, we will add it as part of form. But we don't want users see it, so we use <input type="hidden">, these input fields are just like other input field, but invisible to users.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  10. #10
    Join Date
    May 2014
    Posts
    21

    Default

    so in this code the value of the secret number is echoed where??

    i mean... this code is the one to print the value of SecretNumber so does it print the value in the server memory or something? :P

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •