Cookies in PHP: wht is it and importance?

· Dev & Design
Authors

After a request from script or a server user’s browser stores small amount of data, This data is called cookie.


Cookie can be accessed in three ways by a PHP script:

In a cookie there are information about a name, value, expiry date, host and path.
As cookies are sent from server through HTTP header, they end up to the user.
Here are the 3 ways to access cookies:

  • using �$HTTP-COOKIE� which is the environmental variable, all cookie names
    and values are present in this variable.
  • using global variable �$cookie_name�, here the name should be replaced
  • using �HTTP_COOKIE_VARS [�cookie_name�]� which is a global array variable.
    (here replace the �cookie_name� by actual name of the cookie).
print $HTTP_COOKIE; //outputs �visits=23�
print getenv(�HTTP_COOKIER�); //outputs �visits=23�
print $visits; //outputs �23�
print $HTTP_COOKIE_VARS[visits]; //outputs �23�


How to Set a Cookie with PHP

�header()� function, or �setcookie()� function can be used to set cookie with PHP.
The main purpose of �header()� function is not to set a cookie, and works
just like �setcookie()�. Cookie header is written by ourself by using �header()� funtion, but �setcookie()� is much automated.

//don�t output anything before this…
header(�visits=23; expires=Friday, 15-Nov-06 03:27:21 GMT; path=/;
domain=softwareprojects.org�);
setcookie(�hits�, 23, time() + 3600, �/�, �softwareprojects.org�, 0);
//notice this last extra argument

Weather the cookies will be send over a secure connection or not is denoted
by the last argument to “setcookie()” function. Here “0” means no and “1” means yes.


How to Retrieve a Cookie Value

To retrieve a cookie value, PHP $_COOKIE variable is used.
In the following example the value of cookie named “user” is retrieved and displayed on the page.

<?php
// Print a cookie
echo $_COOKIE[“user”];

// A way to view all cookies
print_r($_COOKIE);
?>


How to Delete a cookies

To delete a cookie we should SET the cookie we want to delete with the date that has
already expired. While doing so we should include the same path, secure parameters and domain
which was originally used to set the cookie.

setcookie(�visits�, 23, time() – 60, �/�,�vyom.co.in�, 0)

Weather the cookies will be send over a secure connection or not is denoted
by the last argument to “setcookie()” function. Here “0” means no and “1” means yes.


Limitations of cookies

Apart from the advantage of passing information from one page to another page
or visit to visit, Cookies do have some limitations. The maximum number of cookies that
can be stored by the browser is 20.And maximum size of the cookie is 4KB.
The user’s privacy is maintained since only the originating host can read the data that has been stored.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s