Home » Programming » php » PHP Cookies

PHP Cookies

The cookie is a mechanism to store data in the browser for a long time. It is a small data that server embeds on the browser. When a user opens any sites or does some activities in it and next time when he/she open the same website server can identify easily the user. Because browser stores the previous data in a small file which name is the cookie.
The first-time browser sends a request to the server
Second server returns response + cookies to the browser.
Third when the same request sent from browser to server then browser send cookie file also with the request.

Practical Application of Cookie in Real life activity:
Now, this day’s maximum user use online shopping through the e-commerce site. If any user opens any shopping site in his/her computer browser and adds some product in the cart list, then next time when he/she open those site again from his/her computer browser he/she can see the product in the cart list as it is. This is one of the applications of Cookies.

Using cookie any website can track the visitor. Like visitor IP, website open time, spending time on this website or the particular on by one page of this website, next return time of old visitors etc.

Remember user email id and password is another application of cookies.

Creating and accessing cookie in PHP

PHP allows creating the cookie and retrieving cookie value. setcookie() function is used to create the cookie in PHP.
Syntax of setcookie() function:

i. Name : Cookie file name where store information or data. It is any variable name or any string value. Which used later to retrieve cookie value. This is the required parameter.
ii. Value: The actual data which is stored in the cookie. Like username, password etc. Here can be set null value also.
iii. Expiry: Duration means how many minutes, hours, days or months cookie will exist. It is the future time in second value after that cookie will automatically delete. If this field is not set then the value will automatically delete when the browser is closed.
iv. Path: This field stands for the directories for which cookie is valid. A single forward slash (/) denote cookie to be valid for all directories.
v. Domain: This is the option for specifying the domain name, so all cookies are valid for this host and domain which created them.
vi. Security: Here set 1 value to specify that the cookie should be sent by secure site using HTTPS or set 0 to specify cookie can be sent from any regular HTTP.

Example
set.php

retrieve.php

Output

Explain

After execute the set.php in the browser a small cookie file generate in the browser settings and Black value set in the favcolor variable for next 1 minute. User can check the cookie file in their browser, go to the browser settings -> Advanced-> Privacy and security-> Content settings-> Cookies-> See all cookie and site data->

There all browser cookie store with the cookie name.

In this example favcolor variable set ‘Black’ value for 1minute. So retrieve.php refresh in the browser after one minute it will return ‘Favorite Color is’. Value will be automatically deleted. Again run the set.php the cookie will be set again for next 1 minute.

Note: time() is a PHP function which returns the current time in second value. Time is calculated from 1970 1st January.

Retrieve cookies

$_COOKIE is a super global variable in PHP. This variable is used to retrieve cookie value. It is the global variable so always declear it in capital letter.

Its return all cookie value used in the current web application as an array. Using cookie a value can be access in multiple pages in a particular web application. In above example, $_COOKIE[‘favcolor’] return Black value any pages in this application. Like print, $_COOKIE[‘favcolor’] in another page along with retrieve.php then also return the same value for 1 minute.

Manually Delete or Destroy Cookie

The cookie is automatically deleted from the browser after complete the expiry time. If expiry time is not set then the cookie will automatically delete when the browser is closed. But need to delete cookie value without the closing browser or before expiry time then set this cookie value for past time.

Example

destroy_cookie.php

Explanation

Set again cookie and the run this file before 1 minute cookie will deleted from the browser automatically then call retrieve.php, its return blank value.

Application of Cookie
In user first login, user enter their username and password and checked remember me option that’s why next time username and password automatically show in the field and do not need to re type username and password again. This mechanism is possible in cookie.

login.php

Please follow and like us:
error