Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

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

  1. #11
    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.

  2. #12
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default Guess my number PHP game code

    http://php.flashwebhost.com/sibichan..._my_number.php

    PHP Code:

    <?php 

    session_start
    (); 
    if (isset(
    $_SESSION['luckyNumber'])) { 

        
    $luckyNumber $_SESSION['luckyNumber']; 

    } else { 

        
    $luckyNumber rand(1,100); 

        
    $_SESSION['luckyNumber'] = $luckyNumber; } 
    if (isset(
    $_POST['userNumber'])) { 

        if (
    $_POST['userNumber'] == $luckyNumber) { 

            
    $_SESSION['luckyNumber'] = rand(1,100);     } } ?> 
    <html> <body> <center><h1>GUESS MY LUCKY NUMBER</h1> 

    <?php 

    $guessAttempt 
    = (isset($_POST['guessAttempt'])) ? $_POST['guessAttempt'] + 0;
    if (isset(
    $_POST['userNumber'])) { 

        
    $userNumber $_POST['userNumber']; 

        if (
    $luckyNumber $userNumber) { 

            echo 
    '<p style="color:blue;font-weight:bold">Try again. ' $userNumber ' is too small.</p>';     } else if ($luckyNumber $userNumber) { 

            echo 
    '<p style="color:blue;font-weight:bold">Try again. ' $userNumber ' is too high.</p>'

        } else { 

            echo 
    '<p style="color:green;font-weight:bold">Well done! You got it right! .</p>' $counter ;     } 

        } 
    ?> 
    <p><strong>No of Guess attempt :</strong> <?php echo $guessAttempt?></p>

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

    Enter Number: <input name="userNumber" type="text"> 

    <input type="hidden" name="guessAttempt" value="<?php echo $guessAttempt?>">

    <button type="submit">Guess</button> 

    </form> </center>

    </body> 

    </html>


    How to do $guessAttempt using $_SESSION ?

    http://php.flashwebhost.com/sibichan...er_session.php

    I tried i like below and removed (<input type="hidden" name="guessAttempt" value="<?php echo $guessAttempt; ?>">) from <form> . Its not working.

    PHP Code:

    if(isset($_SESSION['guessAttempt'])){

    $guessAttempt $_SESSION['guessAttempt'];

    }else {

        
    $guessAttempt $_POST['guessAttempt'] + ;} 


    HERE is the full code ($guessAttempt variable stored in $_SESSION).

    No of Guess attempt showing as 1. The number is not increasing.

    PHP Code:

    <?php 

    session_start
    (); 
    if(isset(
    $_SESSION['guessAttempt'])){

    $guessAttempt $_SESSION['guessAttempt'];

    }else {

        
    $guessAttempt $_POST['guessAttempt'] + ;}
    if (isset(
    $_SESSION['luckyNumber'])) { 

        
    $luckyNumber $_SESSION['luckyNumber']; 

    } else { 

        
    $luckyNumber rand(1,100); 

        
    $_SESSION['luckyNumber'] = $luckyNumber


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

        if (
    $_POST['userNumber'] == $luckyNumber) { 

            
    $_SESSION['luckyNumber'] = rand(1,100); 

        } 



    ?> 

    <html> <body> <center>

    <h1>GUESS MY LUCKY NUMBER</h1> <?php if (isset($_POST['userNumber'])) { 

        
    $userNumber $_POST['userNumber']; 

        if (
    $luckyNumber $userNumber) { 

            echo 
    '<p style="color:blue;font-weight:bold">Try again. ' $userNumber ' is too small.</p>'

        } else if (
    $luckyNumber $userNumber) { 

            echo 
    '<p style="color:blue;font-weight:bold">Try again. ' $userNumber ' is too high.</p>';
         } else { 

            echo 
    '<p style="color:green;font-weight:bold">Well done! You got it right! .</p>' $counter ;     } 

        } 

    ?> 
    <p><strong>No of Guess attempt :</strong> <?php echo $guessAttempt?></p><form method="POST" action=""> Enter Number: <input name="userNumber" type="text"> <button type="submit">Guess</button> </form> </center>
    </body> </html>
    Last edited by Vahaa11; 05-24-2014 at 09:35 AM.

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

    Default

    http://php.flashwebhost.com/sherly/secret_number1.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>
    
    <center><h1>Number Guessing Game.</h1></center>
    <?php
    
    if (isset($_POST['userNumber'])) {
        $userNumber = $_POST['userNumber'];
        if ($secretNumber > $userNumber) {
            echo '<center><h1 style="color:red">Your number is too SMALL.</h1></center>';
        } else if ($secretNumber < $userNumber) {
            echo '<center><h1 style="color:blue">Your number is too BIG.</h1></center>';
        } else {
            echo '<center><h1 style="color:green">You win the game.</h1></center>';
        }
    }
    ?>
    
    <form method="POST" action="">
    <center>Enter Number: <input name="userNumber" type="text"></center>
    <center><button type="submit">Check</button></center>
    </form>
    
    </body>
    </html>

  4. #14
    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;
    }

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

    Default

    Quote Originally Posted by minisoji View Post
    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;
    }
    When you have an error or don't understand any part of the code, ask in the forum, Some one will be able to explain it different way or point out where the bug is. Don't get stuck with some thing, just ask. Everyone get stuck at some point, a good understanding team can help each other and find solution to problem much faster.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  6. #16
    Join Date
    Apr 2005
    Posts
    46,704

    Default

    http://php.flashwebhost.com/mini/session.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 '<h2 style="color:#FFA500"><blink>Number below secret number.</blink></h2>';
        } else if ($secretNumber < $userNumber) {
            echo '<h2 style="color:#B80000 "><blink>Number above secret number.</blink></h2>';
        } else {
            echo '<h1 style="color:#FF00FF"><blink>Win!</blink></h1>';
        }
    }
    
    ?>
    
    <form method="POST" action="">
        Enter Number: <input name="userNumber" type="text">
        <button type="submit">Click here</button>
    </form>
    
    </body>
    </html>
    Last edited by minisoji; 05-27-2014 at 09:07 AM.

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

    Default

    Updated first post with updated script that will display number of try required to get the correct answer. It is ok if you don't understand that part, just skip it.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  8. #18
    Join Date
    Jan 2008
    Location
    india,kerala-god's own country
    Posts
    14,007

    Default

    http://php.flashwebhost.com/ramesh/day-11/session.php

    PHP Code:
    <?php

    session_start
    ();
    if (isset(
    $_SESSION['forum_Counter'])) {
    echo 
    '<p>Welcome, you visited our Forum ' $_SESSION['forum_Counter'] . ' times before.</p>';
     
    $_SESSION['forum_Counter'] = $_SESSION['forum_Counter'] + 1;
    } else {
        echo 
    '<p>Welcome, this is your first visit our Forum.</p>';
        
    $_SESSION['forum_Counter'] = 1;
    }

  9. #19
    Join Date
    Feb 2007
    Posts
    26,214

    Default

    DAY 11 PHP PROGRAMMING

    http://php.flashwebhost.com/tom/day_11_ex_1.php

    PHP Code:

    <?php
    session_start
    ();
    if (isset(
    $_SESSION['visitCounter'])) {
    echo 
    'Welcome, You Visited this Page ' $_SESSION['visitCounter']. ' Times.';
    $_SESSION['visitCounter'] = $_SESSION['visitCounter'] + 1;
    } else {
    echo 
    '<p>Welcome, This is Your First Visit to this Page.</p>';
    $_SESSION['visitCounter'] = ;
    }

  10. #20
    Join Date
    Feb 2007
    Posts
    26,214

    Default

    DAY 11 PHP PROGRAMMING

    http://php.flashwebhost.com/tom/day_11_ex_2_test.php

    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 
    'Your number is too small';
    } else if (
    $secretNumber $userNumber) {
    echo 
    'Your number is too big';
    } else {
    echo 
    'You win the game';
    }
    }
    ?>
    <form method="POST" action="">Enter Number: <input name="userNumber" type="text"><button type="submit">Enter</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>

Page 2 of 2 FirstFirst 12

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
  •