PHP – Cookies

Cookies are text files that allow programmers to store some information on the client computer, and they are kept for tracking purposes. PHP transparently supports HTTP cookies.

3 steps involved in identifying returning users −

  • Server scripts send a set of cookies to the browser. For example, name, age or identification number etc.
  • For future reference, the browser stores this information on a local machine.
  • The next time the browser sends a request to the web server, it sends this cookie information to the server and the server uses this information to identify the user

This post will teaches you how to set cookies, access and how to delete them.

The Anatomy of a Cookie

Cookies are usually set in an HTTP header (although JavaScript can also set a cookie directly in a browser). A PHP script that sets a cookie may send headers that look like this –

PHP - Cookies | The Anatomy of a CookieAs you can see, the set-cookie header consists of a name value pair, a GMT date, a path, and a domain. Name and value will be URL encoded. The expiration box is an instruction to the browser to “forget” the cookie by the given time and date.

If the browser is configured to store cookies, it will hold this information until the expiration date. If the user points the browser to any page matching the path and domain of the cookie, it will send the cookie to the server. The browser headers might look like this –

The Anatomy of a CookieA PHP script then accesses the cookie in the $ _COOKIE or $ HTTP_COOKIE_VARS [] environment variables, which contain all cookie names and values. The above cookie can be accessed using $ HTTP_COOKIE_VARS [“name”].

Setting Cookies with PHP

PHP provided the cookie () function to set the cookie. This feature requires up to six arguments and must be called before the <html> tag. For each cookie, this feature must be called separately.

set cookie (name, value, expiration, path, domain, security)

Here are the details of all the arguments −

Name – This sets the name of the cookie and is put away in a domain variable called HTTP_COOKIE_VARS. This variable is used during access to cookies.

Value – This sets the value of the named variable and is the content you want to actually store.

Expiry – This indicates a future time in seconds since 00:00:00 GMT on January 1, 1970. After this time the cookie becomes unavailable. If this parameter is not set, the cookie expires automatically when the web browser is closed.

Path – This specifies the folders for which the cookie is valid. A single slash allows the cookie to be valid for all folders.

Domain – It can be used to specify domain names in very large domains and must have at least two periods to be valid. All cookies are only eligible for the host and domain that created them.

Security – This can be set to 1 to specify that the cookie should only be sent with secure transmission using HTTPS, otherwise set to 0, which means the cookie can be sent using regular HTTP.

Following example will create two cookies name and age these cookies will expire after one hour.

Setting Cookies with PHP

Accessing Cookies with PHP

PHP provides many ways to access cookies. The simplest approach is to use the $ _COOKIE or $ HTTP_COOKIE_VARS variable.

Accessing Cookies with PHPThe following example accesses all cookies listed in above example.

(PHP-Cookies) Accessing Cookies with PHPYou can use the isset () function to check if a cookie is set.

Deleting Cookie with PHP

Officially, to delete a cookie, you only need to call setcookie () with the name argument, but this does not always work well and should not be dependent on.

It is safest to set a cookie with an already expired date.

Deleting Cookie with PHP

Related Video

Article shared by PHP training in Chandigarh

Leave a Reply