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

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

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

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

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
  •