PHP Cookies

php transparently supports http cookies. cookies are a mechanism for storing data in the remote browser and thus tracking return users.


uses of cookie

  • session management
  • user identification
  • tracking : cookies are used to track the user.

Create a Cookie in php script

the php setcookie() method is used to set the cookie. this function return true on success otherwise return false.

note : the setcookie() function must be called before any output is sent to the browser.

syntax:

         setcookie(name, value, expire, domain, secure, httponly)
  • name : specifies the name of the cookie.
  • value : specifies the value of the cookie.
  • expire : specifies when the cookie expires.

in the example we will create a cookie named "first_name" and assign the value "sandeep" to it and specify the cookie should expire after two hour.

<?php  // the setcookie() function must appear before the <html> tag
     
              setcookie("first_name", "sandeep", time()+7200);
              
  ?>
            
            <html>    <head>  </head> </html>
            

the setcookie() function sends a cookie by urlencoding the cookie value.

the setrawcookie() function sends a cookie without urlencoding the cookie value.

syntax:

         setrawcookie( name, value, expire, path, domain, secure)

PHP $_COOKIE variable

the php $_cookie superglobal variable is used to retrieve a cookie value.

syntax:

         $_global['cookie_name']

in this example we retrieve the value of the cookie named "firstname" and display it:

<?php  
   
                   echo  "firstname cookie value =: ". $_cookie["first_name"] ;
                   
 ?>
              
                  if you wish to retreive all the cookies, you may use this code
                  
         <?php 
         
                  print_r( $_cookie );
             ?>
<?php $first_name = "sandeep";

         $last_name  = "kumar nehra";

         $age  = "25";

               setcookie( "first_name", $first_name, time()+3600);
               
               setcookie( "last_name", $last_name, time()+3600);
               
               setcookie( "age", $age, time()+3600);
?>
     
          
<?php
         // after the page reloads, print them out
         
                 print_r($_cookie);
        ?>

how to delete a cookie ?

to delete a cookie value, you may set the expire time of the cookie in the past.

in this example we delete a cookie which already sets.

<?php setcookie("first_name", "sandeep", time()-3600);
           
           setcookie("last_name", "kumar nehra", time()-3600);
           
           setcookie("age", "25", time()-3600);
           
   ?>

when using cookies, there are a few things you should keep in mind:

  • Cookies must be set before any other output is sent from the PHP script or else you will    receive an error.
  • A server can define multiple cookies with different names, but browsers limit the number of    cookies per server
  • The maximum size of any cookie is 4KB.
  • A user can turn cookies off in their browser.