Set Cookies Get Cookies Delete Cookies with PHP

Cookies are small files created by server on a user computer, cookies are primarily used to store little information about anything, and when user visits your website next time the information stored in cookies can be retrieved and used in various purpose.

Uses of cookies

Cookies can be used for various purpose such as:
  • Store user preferences (color, font size, element size, theme etc.)
  • Track progress (page number, percentage, etc.)
  • Store username or numbers (to identify user)

Set Cookie

We can create a cookie on a user computer using PHP setcookie(). This function requires commonly 3 parameter to create a cookie, name, value and expiration time.
PHP
  • 1
  • 2
$cookie_val = 'Chrome'; //variable setcookie("browser", $cookie_val, time()+3600); //cookie name, value & time (1 hour)
If you run this code, it will create a cookie named "browser" with its value "Chrome" on user computer, and this cookie will expire after an hour. Similarly we can create cookies with different values and different expiration time, which can be retrieved later for other use.A cookie can be created with more advance parameters, which includes path, domain, secure, httponly. setcookie(NAME, VALUE, EXPIRE, PATH, DOMAIN, SECURE, HTTPONLY)
PHP
  • 1
  • 2
$cookie_val = 'Chrome'; //variable setcookie("browser", $cookie_val, time()+3600, '/', 'sanwebe.com', true, true);
If you set '/' in path the cookie will be available within entire domain, if you set to something like '/mfolder/' the cookie will be available within '/mfolder/' and its sub-directories. Likewise set the domain that the cookie will be available to.

Retrieve Cookie Value

Once the cookies have been created, we can simply retrieve the value of our cookie from any page with predefined PHP variable called $_COOKIE. The code below displays the value of the cookie on the browser.
PHP
  • 1
echo 'Your browser is ' . $_COOKIE['browser'];
The code above will end up with an error, if it doesn't find cookie named "browser", therefore it's a good idea to check whether cookie exist or not before trying to output its value.
PHP
  • 1
  • 2
  • 3
  • 4
if(isset($_COOKIE['browser'])) //check cookie exist { echo 'Your browser is ' . $_COOKIE['browser']; }
Or a shorthand way :
PHP
  • 1
  • 2
$cookie_value = (isset($_COOKIE['browser']))? $_COOKIE['browser'] :'no cookie'; echo $cookie_value ;
The cookies stored in client browser will continue to exist until the expiration date or when user decideds to clear all cookies. But if you wish to delete your cookie before that time, you can just add another line in your script as shown in example below.

Delete a Cookie

It is fairly easy to delete a cookie stored in your computer. We just have to set expiration date to the past :
PHP
  • 1
setcookie("browser", "", time()-3600); //expiration time set to one hour ago
When you execute this code, it will overwrite the expiration time of cookie named "browser" to one hour ago, which will trigger the removal mechanism in your browser causing this particular cookie to be removed instantly from the browser.
New question is currently disabled!