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

    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.

  2. #2
    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>

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

    Default

    Quote Originally Posted by sherlyk View Post
    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>

    How this works ?

    PHP Code:
     echo '<h1 style="color:brown">Sorry you Lose !, The right number is.</h1>'
    What is the right number ? It just say sorry, Sorry you Lose !, The right number is .............and did not print any Number, that too after i find correct number,
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

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

    Default

    http://php.flashwebhost.com/annie/numbergame.php
    Code:
    <?php
    session_start();
    if (isset($_SESSION['secretNumber'])) {
    $secretNumber = $_SESSION['secretNumber'];
    } else {
    $secretNumber = rand(1,100);
    $_SESSION['secretNumber'] = $secretNumber;
    }
    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> 
    
    </body>
    </html>

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

    Default

    Quote Originally Posted by annie View Post
    http://php.flashwebhost.com/annie/numbergame.php
    Code:
    <?php
    session_start();
    if (isset($_SESSION['secretNumber'])) {
    $secretNumber = $_SESSION['secretNumber'];
    } else {
    $secretNumber = rand(1,100);
    $_SESSION['secretNumber'] = $secretNumber;
    }
    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> 
    
    </body>
    </html>
    Nice, working properly. Try to understand this code.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

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

    Default

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

    Code:
    <?php
    
    
    session_start();
    
    
    if (isset($_SESSION['foodcounter'])) {
    
    
        echo '<p>Welcome, you visited this page ' . $_SESSION['foodcounter'] . ' times before.</p>';
        $_SESSION['foodcounter'] = $_SESSION['foodcounter'] + 1;
        echo '<p>we have all types of food here american, african, japanese, chines, italian, indian and many more.</p>';
        echo '<p>OFFER, FREE HOME DELIVERY, if we are even a second  later than 30 min the food is free.</p>';
    } else {
    
    
        echo '<p>Welcome, this is your first visit to our website.</p>';
        
        $_SESSION['foodcounter'] = 1;
    
    
    }

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

    Default

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

    Code:
    <?php
    
    
    session_start();
    
    
    if (isset($_SESSION['foodcounter'])) {
    
    
        echo '<p>Welcome, you visited this page ' . $_SESSION['foodcounter'] . ' times before.</p>';
        $_SESSION['foodcounter'] = $_SESSION['foodcounter'] + 1;
        echo '<p>we have all types of food here american, african, japanese, chines, italian, indian and many more.</p>';
        echo '<p>OFFER, FREE HOME DELIVERY, if we are even a second  later than 30 min the food is free.</p>';
    } else {
    
    
        echo '<p>Welcome, this is your first visit to our website.</p>';
        
        $_SESSION['foodcounter'] = 1;
    
    
    }
    Come after 31 minutes please.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  8. #8
    Join Date
    Apr 2005
    Posts
    46,704

    Default

    http://php.flashwebhost.com/mini/1.php

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

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
  •