PHP – Cookies

PHP – Cookies,Cookies are textual content files stored at the purchaser pc and they’re kept of use tracking cause. PHP transparently helps HTTP cookies.

There are 3 steps involved in figuring out returning customers −

  • Server script sends a hard and fast of cookies to the browser. For instance call, age, or identity variety and many others.
  • Browser stores this facts on nearby machine for destiny use.
  • When subsequent time browser sends any request to net server then it sends the ones cookies records to the server and server makes use of that statistics to discover the user.

This bankruptcy will train you the way to set cookies, a way to access them and a way to delete them.

PHP – Cookies,The Anatomy of a Cookie

Cookies are usually set in an HTTP header (even though JavaScript can also set a cookie immediately on a browser). A PHP script that units a cookie might send headers that look some thing like this −

HTTP/1.1 200 OK
Date: Fri, 04 Feb 2000 21:03:38 GMT
Server: Apache/1.3.9 (UNIX) PHP/4.0b3
Set-Cookie: name=xyz; expires=Friday, 04-Feb-07 22:03:38 GMT; 
                 path=/; domain=tutorialspoint.com
Connection: close
Content-Type: text/html

As you could see, the Set-Cookie header includes a call value pair, a GMT date, a route and a website. The name and cost might be URL encoded. The expires discipline is an guidance to the browser to “neglect” the cookie after the given time and date.

If the browser is configured to shop cookies, it will then preserve this statistics until the expiry date. If the user factors the browser at any page that fits the course and domain of the cookie, it’ll resend the cookie to the server.The browser’s headers would possibly appearance something like this −

GET / HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc)
Host: zink.demon.co.uk:1126
Accept: image/gif, */*
Accept-Encoding: gzip
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8
Cookie: name=xyz

A PHP script will then have get admission to to the cookie within the environmental variables $_COOKIE or $HTTP_COOKIE_VARS[] which holds all cookie names and values. Above cookie can be accessed the use of $HTTP_COOKIE_VARS[“name”].

PHP – Cookies,Setting Cookies with PHP

PHP furnished setcookie() feature to set a cookie. This characteristic requires upto six arguments and must be referred to as earlier than tag. For every cookie this characteristic needs to be known as separately.

setcookie(name, value, expire, path, domain, security);

Here is the detail of all the arguments −

  • Name − This units the call of the cookie and is stored in an surroundings variable referred to as HTTP_COOKIE_VARS. This variable is used even as getting access to cookies.
  • Value − This units the cost of the named variable and is the content which you absolutely need to shop.
  • Expiry − This specify a destiny time in seconds because 00:00:00 GMT on 1st Jan 1970. After this time cookie will become inaccessible. If this parameter is not set then cookie will routinely expire while the Web Browser is closed.
  • Path − This specifies the directories for which the cookie is legitimate. A unmarried forward minimize person lets in the cookie to be valid for all directories.
  • Domain − This can be used to specify the area name in very large domain names and have to contain at least two intervals to be valid. All cookies are best valid for the host and domain which created them.
  • Security − This can be set to 1 to specify that the cookie need to handiest be sent by way of comfy transmission the use of HTTPS in any other case set to 0 which mean cookie can be sent by means of everyday HTTP.

Following example will create two cookies name and age these cookies may be expired after one hour.

<?php
   setcookie("name", "John Watkin", time()+3600, "/","", 0);
   setcookie("age", "36", time()+3600, "/", "",  0);
?>
<html>
   
   <head>
      <title>Setting Cookies with PHP</title>
   </head>
   
   <body>
      <?php echo "Set Cookies"?>
   </body>
   
</html>

PHP – Cookies,Accessing Cookies with PHP

PHP provides many approaches to get admission to cookies. Simplest manner is to use either $_COOKIE or $HTTP_COOKIE_VARS variables. Following example will get right of entry to all the cookies set in above example.

<html>
   
   <head>
      <title>Accessing Cookies with PHP</title>
   </head>
   
   <body>
      
      <?php
         echo $_COOKIE["name"]. "<br />";
         
         /* is equivalent to */
         echo $HTTP_COOKIE_VARS["name"]. "<br />";
         
         echo $_COOKIE["age"] . "<br />";
         
         /* is equivalent to */
         echo $HTTP_COOKIE_VARS["age"] . "<br />";
      ?>
      
   </body>
</html>

You can use isset() function to check if a cookie is set or not.

<html>
   
   <head>
      <title>Accessing Cookies with PHP</title>
   </head>
   
   <body>
      
      <?php
         if( isset($_COOKIE["name"]))
            echo "Welcome " . $_COOKIE["name"] . "<br />";
         
         else
            echo "Sorry... Not recognized" . "<br />";
      ?>
      
   </body>
</html>

Deleting Cookie with PHP

Officially, to delete a cookie you need to name setcookie() with the name argument best however this doesn’t continually paintings well, however, and need to not be depended on.

It is most secure to set the cookie with a date that has already expired −

<?php
   setcookie( "name", "", time()- 60, "/","", 0);
   setcookie( "age", "", time()- 60, "/","", 0);
?>
<html>
   
   <head>
      <title>Deleting Cookies with PHP</title>
   </head>
   
   <body>
      <?php echo "Deleted Cookies" ?>
   </body>
   
</html>