Monday, April 19, 2010

PHP Cookies

All about Cookies


Cookie is nothing but a small text file that is embeded by server into user's computers each time the computer requests the page through browser.

Cookies can store information in them and they are stored on client side. This way they help us in many ways like storing user preferences, visited sites, used to identify users etc ...

How to Create a Cookie?
We can basically set a cookie by using setcookie() function.

bool setcookie( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

  • name – name of the cookie.
  • value - The value of the cookie. Assuming the name is 'cookiename', this value is retrieved through $_COOKIE['cookiename']
  • expire - The time the cookie expires. Time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes)
  • path - The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. The default value is the current directory that the cookie is being set in.
  • domain - The domain that the cookie is available. Example : '.example.com'
  • secure - When set to TRUE, the cookie will only be set if a secure connection exists.


Setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including html and head tags as well as any whitespace.
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.

setcookie("myCookie", "PHP Tutorial", time()+3600, "/tutorials");

How to Retrieve a Cookie Date?
Now the cookie is set and we need to retrieve the information. As mentioned above the name of each cookie sent by your server accessed with the superglobal array $_COOKIE. In the example below we retrieve the value of the cookie and print out its value on the screen.

echo "The cookie value is ".$_COOKIE['myCookie'];

This would show up on the page as: "myCookie value is PHP Tutorial".

How to Delete a Cookie?
By default, the cookies are set to be deleted when the browser is closed. We can override that default by setting a time for the cookie's expiration but there may be occasions when you need to delete a cookie before the user closes his browser, and before its expiration time arrives. To do so, you should assure that the expiration date is in the past. The example below demonstrates how to do it (setting expiration time 1 minute ago):

setcookie("myCookie", "", time()-60);

What Are Cookies Used For?
One use of cookies is for storing passwords and user ID's for specific web sites. Also, they are used to store preferences of start pages. On sites with personalized viewing, your web browser will be requested to utilize a small amount of space on your computer's hard drive to store these preferences. That way, each time you log on to that web site, your browser will check to see if you have
any pre-defined preferences (a cookie) for that unique server. If you do, the browser will send the cookie to the server along with your request for a web page. Microsoft and Netscape use cookies to create personal start pages on their web sites. Common uses for which companies utilize cookies include: on-line ordering systems, site personalization, and web site tracking.
Site personalization is one of the most beneficial uses for cookies. For example, a person comes to the CNN site, but does not want to see any business news. The site allows the person to select this choice as an option. From then on (or until the cookie expires), the person would not see business news when they access the CNN web pages.

No comments:

Post a Comment