April 9, 2010

How to use Cookies in PHP with examples


A cookie is a message given to a Web browser by a Web server. The browser stores that message in a small text file .Each time the same computer requests a page with a browser, the cookie is sent back to the server too. You can do number of things with cookies. They are used to store information about user, visited pages, poll results and etc. The main purpose of cookies is to identify users and possibly prepare customized Web pages for them.

Generally cookies are used only to store small amounts of data. Websites can read the values from the cookies and use the information as desired. In addition to the information it stores, each cookie has a set of attributes that helps ensure the browser sends the correct cookie when a request to a server is made.

Even though cookies are not harmful some people do not permit cookies due to concerns about their privacy. In this case you have to use Sessions.

How to Create a Cookie?

PHP cookies can be set using the setcookie() function. The syntax is as follows:
setcookie($name, $value, $expire, $path, $domain, $secure) 

  • $name - It is used to define the cookie name. The name of each cookie sent is stored in the superglobal array $_COOKIE.
  • $value - It is used to define the cookie value. It is associated with the cookie name.
  • $expire - It denotes the time after which the cookie should expire in seconds
  • $path - It specifies the exact path on the domain that can use the cookies.
  • $domain - The domain that the cookie is available. If not domain is specified, the default value is the value of the domain in which cookie was created.
  • $secure - It is used to specify whether the cookie will be sent via HTTPS. A value of 1 specifies that the cookie is sent over a secure connection but it doesn't mean that the cookie is secure. It's just a text file like every other cookie. A value of 0 denotes a standard HTTP transmission.
Creating a cookie with setcookie
<?php
setcookie("username", "David", time()+3600);
?>
<html>
<body>
……
…………
………………
</body>
</html>

Cookie username is set with value David which is set to expire after one hour on users computer.

The function time() retrieves the current timestamp. Appending 3600 seconds (one hour) to the current time to make the cookie to expire after one hour.

Note: A cookie must be set before any HTML code as shown above.Cookies work within HTTP, it's important that you send all cookies before any output from your script. This requires that you place calls to this function before any output, including tags as well as any white space. If you don't do this, PHP will give you a warning and your cookies will not be sent.

Creating a permanent cookie

Lets create a cookie which is set to last for 1 year.

<?php
setcookie("username", "David", time()+(60*60*24*365));
?>

How to Retrieve a Cookie ?

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.

<?php
echo $_COOKIE["username"];
?>

Output =>  David

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):
<?php
setcookie("myCookie", "", time()-60);
?>


Share/Bookmark

1 comments:

Unknown said...

You may find 9A0-066 exam at
http://www.certmagic.com/9A0-066-certification-practice-exams.html Good luck in passing the exam

Post a Comment