Results 1 to 10 of 20

Thread: Day 11 - PHP SESSION - Become PHP Expert in 30 days

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2003
    Posts
    3,040

    Default Day 11 - PHP SESSION - Become PHP Expert in 30 days

    PHP $_SESSION



    With SESSION, data is stored on server side, COOKIE store data on users browser. COOKIE can be edited by user, so it is better use SESSION whenever possible.

    In PHP, SESSION can be accessed with Array $_SESSION

    Like COOKIE, SESSION must be set before you output any text to browser (echo, blank lines etc..).

    Before you can use $_SESSION, you need to start session with PHP function

    PHP Code:
    session_start(); 

    day_11_ex_1.php

    PHP Code:
    <?php

    // Lets start session.
    session_start();

    // check if session variable is set.

    if (isset($_SESSION['hitCounter'])) {
        echo 
    '<p>Welcome, you visited this page ' $_SESSION['hitCounter'] . ' times before.</p>';
        
    // This will increase value of hitCounter by 1 each time you refresh the page.
        
    $_SESSION['hitCounter'] = $_SESSION['hitCounter'] + 1;
    } else {
        echo 
    '<p>Welcome, this is your first visit to this page.</p>';
        
    $_SESSION['hitCounter'] = 1;
    }
    Unlike cookie, SESSION only store data as long as your browser is open. If you close browser, data stored in session will be lost.

    SESSIONS are mostly used to see if a user have logged in to a site or not.

    Number Guessing Game With SESSION

    Let's update our Number Guessing Game code with PHP SESSION support. Now this game is cheat proof, no one can cheat the game and win, try it your self.

    day_11_ex_2.php

    PHP Code:
    <?php

    // We need to call session_start() function before
    // using session.

    session_start();

    // check if session is set, if yes, read secretNumber from session.
    // If no session set, generate a random number, store it in session.

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

    // if user win the game, we need to generate another number
    // and store it in SESSION, so he can play again. If not,
    // a user can play only once, then restart browser to play again.
    // Try commenting code below and see.

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

    ?>
    <html>
    <body>

    <h1>Number Guessing Game.</h1>

    <?php

    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>
    </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>
    What is the proper way to play this game ?

    We know the secretNumber is between 1 and 100, so first try 50.

    If it says userNumber is low, try 75.

    If it says userNumber is high, try 25.

    Continue the same, until you reach the secretNumber. This is called Minimax (MinMax) decision rule.

    Number Guessing Game With Number of Try

    Lets also display number of try required to get the answer. Skip this part if you don't understand.

    day_11_ex_3.php

    PHP Code:
    <?php

    // We need to call session_start() function before
    // using session.

    session_start();

    // check if session is set, if yes, read secretNumber from session.
    // If no session set, generate a random number, store it in session.

    if (isset($_SESSION['secretNumber'])) {
        
    $secretNumber $_SESSION['secretNumber'];
        
    $try $_SESSION['try'];
        
    $_SESSION['try'] = $_SESSION['try'] + 1;
    } else {
        
    $secretNumber rand(1,100);
        
    $try 1;
        
    $_SESSION['secretNumber'] = $secretNumber;
        
    $_SESSION['try'] = $try;
    }

    // if user win the game, we need to generate another number
    // and store it in SESSION, so he can play again. If not,
    // a user can play only once, then restart browser to play again.
    // Try commenting code below and see.

    if (isset($_POST['userNumber'])) {
        if (
    $_POST['userNumber'] == $secretNumber) {
            
    $_SESSION['secretNumber'] = rand(1,100);
            
    $_SESSION['try'] = 1;
        }
    }

    ?>
    <html>
    <body>

    <h1>Number Guessing Game.</h1>

    <?php

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

        echo 
    '<h2>Trail Number ' $try '.</h2>';
    }

    ?>

    <form method="POST" action="">
        Enter Number: <input name="userNumber" type="text">
        <button type="submit">Check</button>
    </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>


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

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

    Default

    http://php.flashwebhost.com/sherly/visitcounter1.php

    Code:
    <?php
    
    // Lets start session.
    session_start();
    
    // check if session variable is set.
    
    if (isset($_SESSION['visitCounter'])) {
    echo '<p>Welcome, you visited our company web page ' . $_SESSION['visitCounter'] . ' times before.</p>';
    // This will increase value of visitCounter by 1 each time you refresh the page.
     $_SESSION['visitCounter'] = $_SESSION['visitCounter'] + 1;
    
    } else {
    
        echo '<p>Welcome, this is your first visit our company web page.</p>';
        $_SESSION['visitCounter'] = 1;
    }

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

    Default

    http://php.flashwebhost.com/sherly/counter1.php

    Code:
    <?php
    
    // Lets start session.
    
    session_start();
    
    // increment a session counter number by 1 each time you refresh the page.
    
    $_SESSION['counternumber']++;
    
    // counter value
    
    echo "You have viewed this page " . $_SESSION['counternumber'] . " times";

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

    Default

    http://php.flashwebhost.com/annie/webcounter.php
    Code:
    <?php
    
    session_start();
    if (isset($_SESSION['webCounter'])) {
    echo '<p>Welcome, you visited this page ' . $_SESSION['webCounter'] . ' times before. </p>';
    $_SESSION['webCounter'] = $_SESSION['webCounter'] + 1 ;
    } else {
    echo '<p> This is your first visit to this page. Welcome! </p>';
    $_SESSION['webCounter'] = 1;
    }

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

    Default

    Quote Originally Posted by annie View Post
    http://php.flashwebhost.com/annie/webcounter.php
    Code:
    <?php
    
    session_start();
    if (isset($_SESSION['webCounter'])) {
    echo '<p>Welcome, you visited this page ' . $_SESSION['webCounter'] . ' times before. </p>';
    $_SESSION['webCounter'] = $_SESSION['webCounter'] + 1 ;
    } else {
    echo '<p> This is your first visit to this page. Welcome! </p>';
    $_SESSION['webCounter'] = 1;
    }
    Good, try other example.

    @sherlyk, good, but don't use incremental operator, we are not here to confuse others, for most of us who know basic maths can figure out how to increase value of a variable by one. No increment operator needed for such a simple task. Lets code naturally.

    Also when you do operation on a variable, it should exists. That is why we use isset() function, if not it will generate PHP error (warning), these are disabled on production servers, but when you code, always go for proper code, that will be easy to debug.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

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

    Default

    http://php.flashwebhost.com/sherly/winnernumber.php

    Code:
    <?php
    
    // We need to call session_start() function before
    // using session.
    
    session_start();
    
    // check if session is set if yes, read winnerNumber from session.
    // If no session set, generate a random number, store it in session.
    
    if (isset($_SESSION['winnerNumber'])) {
        $winnerNumber = $_SESSION['winnerNumber'];
    } else {
        $winnerNumber = rand(1,150);
        $_SESSION['winnerNumber'] = $winnerNumber;
    }
    
    // a user can play only once, then restart browser to play again.
    // Try commenting code below and see.
    
    if (isset($_POST['userNumber'])) {
        if ($_POST['userNumber'] == $winnerNumber) {
            $_SESSION['winnerNumber'] = rand(1,150);
        }
    }
    
    ?>
    <html>
    <body>
    
    <h1>Number Guessing Game.</h1>
    
    <?php
    
    if (isset($_POST['userNumber'])) {
        $userNumber = $_POST['userNumber'];
        if ($winnerNumber > $userNumber) {
            echo '<h1 style="color:yellow"> higher number.</h1>';
        } else if ($winnerNumber < $userNumber) {
            echo '<h1 style="color:green">lower number.</h1>';
        } else {
            echo '<h1 style="color:brown">Sorry you Lose !, The right number is.</h1>';
        }
    }
    
    ?>
    
    <form method="POST" action="">
        Enter Number: <input name="userNumber" type="text">
        <button type="submit">Try Here</button>
    </form>
    
    <h2>How to Play:</h2>
    
    <pre>
    * Server will pick a winner number between 1 and 150.
    * You guess what number it is.
    * If your guess is too high or too low, Server will give you a win.
    * See how many turns it takes you to win!
    </pre>
    
    </body>
    </html>

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
  •