Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Day 10 - Let's Eat Cookie - Become PHP Expert in 30 days

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

    Default Day 10 - Let's Eat Cookie - Become PHP Expert in 30 days

    Let's Eat Cookie



    In our Day 9 code (day_9_ex_1.php) we used POST method, so you won't be able to see the secret number server generated by looking at URL. But if you look source code of the page, you still able to see the secret number.

    Viewing Source Code

    You can view source code of any web page by pressing

    Code:
    CTRL+U


    This will only show HTML code. It won't show any PHP code as PHP is running in server side.



    From the source code, you can see the hidden INPUT field stores the secretNumber.

    Code:
    <input type="hidden" name="secretNumber" value="27">
    How can we remove this number from the HTML code, so no one will be able to see it by viewing source code of our HTML page ? We can do this with the help of COOKIE.

    What is COOKIE

    Cookie is used to store information on visitors computer. Some sites can remember you, so you don't have to login to these site every time because they store a COOKIE on your computer, next time when you visit these sites, web server read the data stored in these COOKIE and authenticate you.

    Data stored in cookie are more difficult to read (not impossible) for most users. Also most sites store encrypted value in the cooke, so even if someone read cookie, they won't be able to know what is stored in the cookie.

    Let's make a script, that will store your name in cookie. Greet you when you visit the page again.

    day_10_ex_1.php

    PHP Code:
    <?php

    if (isset($_COOKIE['phpExpertName'])) {
        echo 
    $_COOKIE['phpExpertName'] . ', Welcome to Bakery, eat some $_COOKIE';
        exit;
    }

    if (isset(
    $_POST['phpExpertName']) && strlen($_POST['phpExpertName']) > 3) {
        
    // time()+3600 = 1 hour, that is cookie will remember your name for 1 hour.
        // time() is a PHP function, that return current time in numeric format.
        // 3600 == number of seconds in 1 hour.
        // So to remember 2 hour, use time() + 3600 * 2
        
    setcookie('phpExpertName'$_POST['phpExpertName'], time()+3600);
        echo 
    'Cookie Set. Close the browser. Revisit this page, I will remember your name';
    } else {

        echo 
    '
            What is your name ?
            <form method="post" action="">
            <input type="text" name="phpExpertName">
            <button type="submit" name="whatever">Enter Web Site</button>
            </form>
        '
    ;
    }
    setcookie() function is used to set cookie. setcookie() must be called before any echo statement.

    You will be able to read data stored in cookie using associative array $_COOKIE.

    How to Delete A Cookie

    A cookie can be delete by setting cookie expiry time to 0.

    day_10_ex_2.php

    PHP Code:
    <?php

    setcookie
    ('phpExpertName'''0);
    echo 
    'Cookie deleted.';
    echo 
    '<br>';
    echo 
    'Visit day_10_ex_1.php, you will see the script will ask you name again, because no cookie is set.';



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

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

    Default

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

    PHP Code:
    <?php

    if (isset($_COOKIE['VisitorName'])) {
        echo 
    'Hi ' $_COOKIE['VisitorName'];
        exit;
    }

    if (isset(
    $_POST['VisitorName']) && strlen($_POST['VisitorName']) > 3) {
        
    setcookie('VisitorName'$_POST['VisitorName'], time() + (86400 7)); // Remember 1 week
        
    echo 'Cookie Set. Close the browser. Revisit this page, I will remember your name for 1 week';
    } else {

        echo 
    '
            What is your name ?
            <form method="post" action="">
            <input type="text" name="VisitorName">
            <button type="submit" name="Submit">Remember Me</button>
            </form>
        '
    ;
    }
    I am getting the following warning message

    Code:
    Warning: Cannot modify header information - headers already sent by (output started at /home/fwhphp/public_html/melbin/set_cookie.php:1) in /home/fwhphp/public_html/melbin/set_cookie.php on line 9
    Why ??
    Last edited by melbin; 05-21-2014 at 01:30 PM. Reason: changed to camelCase
    VIDEO WORLD : LATEST HOLLYWOOD || BOLLYWOOD || SOUTH INDIAN VIDEOS || TRAILERS

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

    Default

    Remove empty line from line number 1.

    setcookie() function should be called before outputting any text to browser.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

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

    Default

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

    Code:
    <?php
    
    if (isset($_COOKIE['bakery_visitor'])) {
        echo $_COOKIE['bakery_visitor'] . ', Welcome to Himalaya Bakery, eat some $_COOKIE';
        exit;
    }
    
    if (isset($_POST['bakery_visitor']) && strlen($_POST['bakery_visitor']) > 5) {
        // time()+2800 = 2 hour, that is cookie will remember your name for 2 hour.
        // time() is a PHP function, that return current time in numric format.
        // 2800 == number of seconds in 2 hour.
        // So to remember 4 hour, use time() + 2800 * 4
        setcookie('bakery_visitor', $_POST['bakery_visitor'], time()+2800);
        echo 'Cookie Set. Close the browser. Revisit this page, I will remember your name';
    } else {
    
        echo '
            What is your name ?
            <form method="post" action="">
            <input type="text" name="bakery_visitor">
            <button type="submit" name="whatever">Enter your name</button>
            </form>
        ';
    }
    Last edited by sherlyk; 05-21-2014 at 10:04 AM.

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

    Default

    Quote Originally Posted by sherlyk View Post
    http://php.flashwebhost.com/sherly/cookies.php

    Code:
    <?php
    
    if (isset($_COOKIE['bakery_visitor'])) {
        echo $_COOKIE['bakery_visitor'] . ', Welcome to Himalaya Bakery, eat some $_COOKIE';
        exit;
    }
    
    if (isset($_POST['bakery_visitor']) && strlen($_POST['bakery_visitor']) > 5) {
        // time()+2800 = 2 hour, that is cookie will remember your name for 2 hour.
        // time() is a PHP function, that return current time in numric format.
        // 3600 == number of seconds in 2 hour.
        // So to remember 4 hour, use time() + 2800 * 4
        setcookie('bakery_visitor', $_POST['bakery_visitor'], time()+2800);
        echo 'Cookie Set. Close the browser. Revisit this page, I will remember your name';
    } else {
    
        echo '
            What is your name ?
            <form method="post" action="">
            <input type="text" name="bakery_visitor">
            <button type="submit" name="whatever">Enter your name</button>
            </form>
        ';
    }
    Good, working perfectly.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

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

    Default

    http://php.flashwebhost.com/annie/cookie.php

    Code:
    <?php
    if (isset($_COOKIE['visitor_name'])) {
    echo $_COOKIE['visitor_name'] . ', Welcome to Meritnation';
    exit;
    }
    
    if (isset($_POST['visitor_name']) && strlen($_POST['visitor_name']) > 3) {
    setcookie('visitor_name',$_POST['visitor_name'], time() + 18000);
    echo 'Cookie Set. Close the browser. Revisit this page, I will remember your name';
    } else {
     echo '
            What is your name ?
            <form method="post" action="">
            <input type="text" name="visitor_name">
            <button type="submit" name="submit">Enter Web Site</button>
            </form>
        ';
    }

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

    Default

    Quote Originally Posted by annie View Post
    http://php.flashwebhost.com/annie/cookie.php

    Code:
    <?php
    if (isset($_COOKIE['visitor_name'])) {
    echo $_COOKIE['visitor_name'] . ', Welcome to Meritnation';
    exit;
    }
    
    if (isset($_POST['visitor_name']) && strlen($_POST['visitor_name']) > 3) {
    setcookie('visitor_name',$_POST['visitor_name'], time() + 18000);
    echo 'Cookie Set. Close the browser. Revisit this page, I will remember your name';
    } else {
     echo '
            What is your name ?
            <form method="post" action="">
            <input type="text" name="visitor_name">
            <button type="submit" name="submit">Enter Web Site</button>
            </form>
        ';
    }
    Nice script, working properly.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

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

    Default

    Do we need to use _ ( Underscore ) if our variable have three words ?
    php_expert_name

    Before we used like

    secretNumber
    userNumber
    cowsCanFly

    Any difference between these two variable type?

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

    Default

    Quote Originally Posted by sibichan1 View Post
    Do we need to use _ ( Underscore ) if our variable have three words ?
    php_expert_name

    Before we used like

    secretNumber
    userNumber
    cowsCanFly

    Any difference between these two variable type?

    It is better we follow one type of coding style. So far PHP had no coding guidelines like many other programming languages. So we used whatever we like, but when working on a team, it is better we follow same coding conventions. In vShare, we use $video_info, $user_name etc.. At that time no such coding guidelines where present and we follow that rule in out code.

    Now PHP have a coding style

    http://www.php-fig.org/psr/psr-2/

    This coding style is supported by many large projects as you can see on home page of that site

    http://www.php-fig.org/

    Laravel, Zend Framework 2, Symfony2, Joomla, Drupal, eZ Publish, phpBB, Doctrine, CakePHP, Composer and many more. This list include popular PHP frameworks like Zend, Symfony and Laravel.

    But we can't change coding style of exisiting project in one day, there are lot of code to change, so it may take time, for newer code, it is better follow a coding style, so everyone do the same, not what each programmer like, this may it easy to read/understand code.

    In the code, i used

    Code:
    $_COOKIE['php_expert_name']
    It is better follow the PHP variable naming convention and use

    Code:
    $_COOKIE['phpExpertName']
    I will update the script to avoid further confusion.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

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

    Default

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

    Code:
    <?php
    
    
    if (isset($_COOKIE['flipkartCustomer'])) {
        echo $_COOKIE['flipkartCustomer'] . ',Welcome to flipkart, SPECIAL OFFER- 50% OFF ON ALL GAMES';
    exit;
    }
    
    
    if (isset($_POST['flipkartCustomer']) && strlen($_POST['flipkartCustomer']) > 5) {
       setcookie('flipkartCustomer', $_POST['flipkartCustomer'], time()+3600);
       echo 'COOKIE Set. Close the browser. Revisit this page, I will remember your name';
    } else {
    
    
    echo'
        Enter your name ?
        <form method="post" action="">
        <input type="text" name="flipkartCustomer">
        <button type="submit" name="whatever">Enter Name</button>
      ';
    }

Page 1 of 3 123 LastLast

Tags for this Thread

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
  •