Back to Top

How to set a Cookie in PHP


A cookie is a small amount of data stored in the visitor’s browser with a request from a server or script.Cookies are like sessions. They both hold a piece of information about a particular visitor.However, cookies are stored on the visitor’s computer, whereas session data is kept on the server.

There are two ways to set the cookie in PHP.

1. Header Function Method

First you can use the header() function to set the Set-Cookie header. The header() function requires a string that will be included into the header section of the server response.

Note: header() must be called before any output is sent to the browser because headers are sent automatically.

header ("Set-Cookie: user=$_POST[email]; expires=Tue, 17-May-12 14:39:58 GMT;path=/;");

Here, Set-Cookie header contains a name/value pair, a path, and a domain

2. SetCookie Function

The setcookie() function is used to set a outputs a Set-Cookie header. Usually, you must call the setcookie( ) function before the page generates any output. This means that setcookie( ) must arrives before any print statements.


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

setcookie() function accepts the cookie name, cookie value, expiration date in UNIX epoch format, path, domain, and integer that should be set to 1 if the cookie is only to be sent over a secure connection.

All the arguments in this function are optional rather than the first (cookie name) argument.

$returnCookie =setcookie("user",$_POST[email]), time()+3600, "/", "", 0);

Also Read:

Cookie Free domain in WordPress
jQuery Cookie Explained

Let’s see the detail of all the arguments:

  1. Name – Its the name of the cookie and this variable is used while accessing cookies.Here,I used cookie name to “user”
  2. Value – Its for the value of named variable and here the value of the cookie is the post value of email address entered by user.
  3. Expiry date – It specifies the expiration time of the cookie and after that time cookie will be expired. Here,the time() function to get the current time stamp and have added 3600 seconds in an hour. Its represents our expiration date
  4. Path – Its define a path of “/” which means that a cookie should be sent for any page within our server environment.
  5. Domain – The domain argument to “” which means that to keep Cookies Within Your Domain
  6. Security – Finally, we pass 0 to setcookie() for indicating that cookies can be sent in an insecure environment.

To access Cookie Values:

Usually, to access the value of the cookie in PHP,$_COOKIE in used.

     echo $_COOKIE['user'];

To Delete Cookie:

Officially, to delete a cookie, you call setcookie() with the name argument only:


This method does not always work Instead of using this method to delete a cookie, to set the cookie with a date so with this you are sure that cookie is expired:

setcookie("user", "", time()-60, "/", "", 0);

Also, make sure that you pass setcookie() the same path, domain, and secure parameters as you did when originally setting the cookie.

I hope you have enjoyed this tutorial. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Comments (3)

  1. No question this is the place to get this info, thkans y’all.

  2. Thanks for the good explanation !

  3. Really nice one! Thanks!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to use JavaScript Promise API?

Posted on 2 years ago


An Introduction to WordPress Action Hooks

Posted on 6 years ago


What is DocType In HTML

Posted on 7 years ago


Hello world

Updated 1 month ago

Nick Carter

In depth Understanding of Laravel Core

Updated 3 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 5 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 6 years ago