PHP Tutorial : Cookies with Example

Leave a Comment
Cookies are text files stored on the client computer and they are kept of use tracking purpose.

The browser stores the message in a small text file that the server embeds on the user's computer.Each time the same computer request a page with a browser,the cookie is sent back to the server too.

Cookies are used to store information about, visited pages, poll results and etc.

The main purpose of cookies is to identify users and possibley prepare customized Web  pages for them.

  • How to create a cookie?
  • How to Retrieve a cookie Data?
  • How to delete a cookie?

How to Create a Cookie?

PHP cookies can be set using the setcookie() function.

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

Here is the detail of all the arguments:

  • Name - This sets the name of the cookie and is stored in an enviroment variable called HTTP_COOKIE_VARS. This variable is used while accessing cookies.
  • Value - This sets the value of the named variable and is the content that you actually want to store.
  • Expiry - This specify a future time in seconds since 00:00:00 GMT on 1st Jan 1970.after this time cookie will become inaccessible.if this parameter is not set then cookie will automatically expire when the Web Browser is closed.
  • Path - This specifies the directories for which the cookie is valid. A single forward slash character permits the cookie to be valid for all directories.
  • Domain - This can be used to specify the domain name in very  large domains and must contain at least two periods to be valid.All cookies are only valid for the host and domain which created them.
  • Security - This can be set to 1 to specify that the cookie should only be sent by secure transmission using HTTPS otherwise set to 0  which mean cookie can be sent by HTTP.

Example:
We will create two cookies name and age these cookies will 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>

How to Retrieve a Cookie a Cookie Date?

PHP provides many ways to access cookies.Simplest way is to use either  $_COOKIE or $HTTP_COOKIE_VARS variables.

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["name"]."<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>

How to Delete a Cookie?

To delete a cookie you should call setcookie () with the name argument only but this does not always work well, however, and should not be relied on.

It is safest 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>

0 comments:

Post a Comment