Back to Top

How to Create and Use Cookies in PHP

How to Create and Use Cookies in PHP

A cookie is a special file which is located in the user’s browser so that Web servers can read from and write to.It contains a name/value pair.

However, you can use cookies also store data on beyond the session because cookies are stored on the browser of visitor’s computer.

How to create Cookie:

In PHP, cookies are set using the setcookie() function, and cookies are read nearly automatically.To create a cookie ,SetCookie function is used with at least two parameters

To use this function, you can specify six parameters, only name is mandetory others are optional:

  1. name – specifies the name (rows) assigned to the Cookie;
  2. value – specifies the value of a variable (string);
  3. Expire – time of expiration of the variable (integer). If this parameter is not specified, the Cookie will be available before the end of the session, that is, until you close your browser. If time is specified, when it comes, Cookie self-destruct.
  4. path – the path to Cookie (string);
  5. domain – a domain (a string). The value is set host name from which the Cookie has been established;
  6. secure – Cookie transmission over a secure HTTPS-connection.

The cookie expires when the expiration date is automatically deleted.

Let’s create an example to create the cookie.

Here setcookie set the name value with time function which finds the current time and adds 60 * 60 * 24, so the cookie will be available for one day and 24 hours, the cookie will be deleted.

How to retrieve Cookie:

After your cookies have been set successfully in user’s browser,you can read a cookie on the next request from that browser.

To retrieve cookie using the name passed when create a cookie and will retrieve the value.

They are kept in superglobal $_COOKIE and $HTTP_COOKIE_VARS with the cookie name as an index, and the value as the value it indexes.

You can retrieve a cookie as follows:

In this example,The first time a script is set Cookie”cookie_name” with value “cookie_value”. At the next line to the script, will display the value of Cookie “cookie_name”, that is, the string “cookie_value”.$_COOKIE is an associative array is an array of all of the cookies that have been sent to the server is stored.

You can use cookie anywhere in your script in PHP.You can retrieve cookie only after cookie has been set,

How to Update Cookie

To change the assigned value in the cookie,same setcookie function will be updating the old assigned value in the cookie.

You will get the same cookie with different values or can say second value.

How to delete Cookie

Sometimes you want to remove Cookies.Deleting a cookie is easy.To delete cookies,there are two methods to use

  1. unset function
  2. setcookie function with previous expire value

Let’s check the unset function

Let’s see the next option to delete the cookie.Here, you can use setcookie(), with the exact same arguments except for the value which should be set to as an empty string or you can set an expiration time in the past to delete the cookie.

DrawBack of cookie in PHP:

  1. Some very old browsers do not support cookies.
  2. Sometimes browser refuses to accept cookies and setcookie() function does not even return a value which indicates acceptance or refusal of the cookie

Leave a Reply

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

Most Popular Posts

An Introduction to WordPress Action Hooks

Posted on 5 years ago

Bhumi

The Readers’ Poll – June 2013

Posted on 4 years ago

Bhumi

Branching and Merging in Git

Posted on 1 week ago

Bhumi

How To Repair MySQL Table

Posted on 6 years ago

Bhumi

Overview of ECMAScript 6 new Features

Updated 18 years ago

Bhumi

Getting started with Amazon Web Services

Updated 18 years ago

Bhumi

Facade implementation in Laravel

Updated 18 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 2 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 3 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 4 years ago

Bhumi