What are Cookies ? Using Cookies in PHP.

Discussion in 'PHP' started by lionaneesh, Jun 16, 2011.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Cookies are basically small files which are stored in User’s Computer. Their main purpose is to hold data specific to a particular client and Website, They can only be accessed either by Web Server or the client computer. This allows the server to access user specific data while saving space on the web server, It allows the server to deliver specific tailored web pages to a particular user.

    Cookies act as a mechanism to store data in the remote browser and thus tracking and identifying return users.

    A Cookie mainly contains data in (key,value) pairs for example (name,aneesh) , (message,cookies are great).

    That’s a lot of theory there now let’s move on how to use cookies using PHP.

    Cookies in PHP



    PHP provides us a with suite of functions for manipulation of cookies.

    Setting of Cookies

    These functions include setcookie() or setrawcookie() . As cookies are a part of HTTP headers so , it’s obvious we need to call setcookie() before sending any data.

    Accessing Cookies

    Any cookies which are sent to the server from the client will be automatically included in $_COOKIE auto – global array.

    These cookies can be accessed using following syntax:-

    Code:
       $_COOKIE['name']; 
    
    Using Cookies in PHP

    The setcookie() function is used for setting of cookies

    Syntax :-
    Code:
      setcookie(name, value, expire, path, domain, secure, httponly);
      
    Note: In the above all arguments except name is optional.

    Example:-
    Code:
      setcookie('name', 'aneesh');
      
    This declares a cookie with name=name and value=aneesh , As no expire time is provided the cookie will be deleted from the browser after ending the session.

    Example involving Expire time:-

    Code:
      $minutes = 20;
      setcookie('name', 'aneesh' , time()+($minutes*60));
    
    As the ‘expire’ time input should be in seconds we multiplied it with ‘60’ as each minute contains 60 seconds.

    Printing:-
    Code:
      echo $_COOKIE['name'];
      
    This would print the value of cookie which we earlier set using setcookie()

    Output:-
    Code:
      aneesh
      

    Making a Simple Application in PHP using Cookies



    Now that we know quite a lot about what are cookies and how do we use them lets use them and make a small PHP Application.

    Visited.php :-

    Code:
      <?php
      // Header Contents
       
      $days = 10;
      $hoursPerDay = 24;
      $minutesPerHour = 60;
      $secondsPerMinute = 60;
       
      if(!isset($_COOKIE['visited']))
      {
          setcookie("visited",1,time()+($days*$hoursPerDay*$minutesPerHour*$secondsPerMinute));
      }
       
      ?>
      <html>
          <head>
              <title>
                  Sample Page
              </title>
          </head>
          <h1>Welcome Visitor</h1>
          <body>
               According to your browser cookies you
               <?php
               if(isset($_COOKIE['visited']))
               {
                  echo '<b>have</b>';
               }
               else
               {
                  echo '<b>have not</b>';
               }
               ?>  visited our site in the last 10 days.   
          </body>
      </html>
      
    Output :-
    1. When visiting for the first time :-
    Code:
      [B][FONT=&quot]Welcome Visitor[/FONT][/B]
      [FONT=&quot]According to your browser cookies you [B]have not[/B] visited our site in the last 10 days.[/FONT]
      
    2. Not visiting for the first time
    Code:
      [B][FONT=&quot]Welcome Visitor[/FONT][/B]
      [FONT=&quot]According to your browser cookies you [B]have[/B] visited our site in the last 10 days.[/FONT]
      
    That’s all for this tutorial , Stay tuned for more
     
  2. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Thanks for accepting my article! and please comment what you feel!
     
  3. seangtz

    seangtz New Member

    Joined:
    Jun 6, 2008
    Messages:
    126
    Likes Received:
    3
    Trophy Points:
    0
    I just curious to know is there any difference between the cookies stored in web browsers and the using computers?
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Cookies are stored in Web browser's on user's computer.
     
  5. seosurendra

    seosurendra Banned

    Joined:
    Jul 14, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Cookies is a small file but i used to data as stored in computer.
     
  6. ankitasharma

    ankitasharma Banned

    Joined:
    Jul 23, 2011
    Messages:
    18
    Likes Received:
    1
    Trophy Points:
    0
    A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.


    How to create the cookie :-

    The setcookie() function is used to set a cookie.

    Note: The setcookie() function must appear BEFORE the <html> tag.

    Syntax:-

    setcookie(name, value, expire, path, domain);
     
  7. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    Very useful thread !
     
  8. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice