Back to Top

How to set a Cookie in PHP

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.

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 cookie.it 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.

SYNTAX

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.

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 “yourdomain.com” 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.

To Delete Cookie:

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

This method does not always work well.so 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:

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 ezSQL with example

Posted on 9 years ago

Bhumi