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

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

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

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

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

  5. #5
    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>
        ';
    }

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

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

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

  9. #9
    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>
      ';
    }

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

    Default

    Quote Originally Posted by stefin View Post
    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>
      ';
    }
    Thank you for the discount.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

Page 1 of 2 12 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
  •