Cookies in PHP:

A cookie is a small text file often used to identify the user. Cookies are stored in client computers. The purpose of creating cookie is use to track the user information.

In PHP you can create a cookie, retrieve cookie and also can set the expiry time for cookie.

How Cookie Works?

There are three steps that are used in cookie to identify user:

  • A set of cookie is send by server to the browser like name, password etc.
  • Than the browser stores that all information on the client PC for further use.
  • So when next time the browser sends the request to the server it will use the information that saved in cookie.

 Create and Retrive Cookies in PHP:

To create a cookie in PHP you have to use “setcookie()” function. To retrieve a cookie you have to use Global variable $_COOKIE[index].

Syntax:

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

The name argument is require and else are optional.

Example:

In following example we are creating a cookie and setting its expiry time for that and also we are retrieving cookie.

 Deleting a Cookie:

If you want to delete a cookie you have to set the expiration date in “setcookie()” function.

Example:

 Note:

You can also modify a cookie by just setting the new value again in setcookie() function.

2 responses to “Cookies in PHP”

  1. Marry Martin says:

    Thank you for the sensible critique. Me and my neighbor were just preparing to do a little research about this. We got a book from our local library but I think I learned better from this post. I am very glad to see such wonderful info being shared freely out there..

  2. I just want to mention I’m very new to blogs and absolutely loved your web-site. More than likely I’m planning to bookmark your blog . You really come with incredible articles and reviews. Regards for sharing with us your web page.

Leave a Reply

Your email address will not be published. Required fields are marked *