Cookies in PHP




How to use cookies in PHP

Coding-zon PHP Cookies



 

Table of Contents

  1. What is Cookie?
  2. Creating Cookie
  3. How to View Cookies?
  4. Retrieving Cookie
  5. Removing Cookie

 

Basically a cookie used to store client data. The website identifies a user with this data.

  •   A cookie is a small variable that the server embeds on the user's computer.
  •   When a page request comes from the user, the server also sends a cookie to the browser along the page URL.
  •   Using PHP, we can create and retrieve cookie values.

  

 PHP Cookies:

Cookie is a super global variable. It is an  associative array and stores user information in the form of name value pairs.  Cookie information available to all pages on the website.

Syntax:

setcookie(cookie_name, cookie_value, expire)
setcookie(cookie_name, cookie_value, expire, path, domain);
 
Example:
setcookie("user", "Shashankh", time()+3600);

expiration time is set to 1hour. current time plus 3600 milliseconds. So the lifetime of the cookie is one hour. After that, the cookie was deleted. Default time of the cookie is 30 days or as set in the browser. However, it is the user choice, he can delete cookies any time.

GoTop


Code Example:

cookie.php

<?php
 
setcookie("user", "Shashankh", time()+3600);
 
 
?>
 

setcookie creates a cookie 'user'. To view cookies in the browser, there are several ways. It depends on the browser that you are using

In this example we are using Chrome browser. To view cookie information in chrome, just follow the steps given below. 

  • Click on Customize and control the chrome  menu option in the top right corner of the chrome.
  • Select More tools and select Developer tools 
  • Select Application tab and Cookies from sidebar
  • Select the Url and  right panel you will the cookies as shown in the 2nd image.

 

GoTOC

Using Chrome:

 



 

Retrieving Cookie: 

 Cookies can be retrieved in PHP using inbuilt array called $_COOKIE.

GoToMenu

Code Example:

cookie.php

<?php
 
setcookie("user", "Shashankh", time()+3600);
 
echo "<br>".$_COOKIE['user'] ."<br>";
 
?>
 
output: 

Shashankh

Example2:

 
 
<?php

setcookie("user", "Shashankh", time()+3600);

setcookie("city", "hyderabad", time()+5);

 
echo "<br>".$_COOKIE['user'] ."<br>";

echo "<br>".$_COOKIE['city'] ."<br>"; 

?> 

output

Shashankh

Hyderabad

In the above example, the cookie variable 'city' will expire after 5 seconds automatically. When you refresh the browser, you will see an undefined variable error.

if refresh the browser  then you will see the following output.

Shashankh

Notice: Undefined index: city in C:\xampp1\htdocs\coding-zon\php-cookies\cookie.php on line 10


MoveTop

Example3:

To view all cookies, use the following code.

 
<?php
 
print_r($_COOKIE);
 
?>
 
 

 Cookies can be removed in several ways.

1) Using setcookie

Example 1:

remove_cookie.php

<?php
 
setcookie("user", "Shashankh", time()+3600); 
 
echo "<br>".$_COOKIE['user'] ."<br>"; // cookie value displayed
 
setcookie("user", "", time()-3600);
 
echo "<br>Cookie deleted.."; 
 
echo "<br>".$_COOKIE['user'] ."<br>";  // error
 
?>

Example 2:

setcookie('user', '', 1); // 1 is just a sample time point in the past.

2) from the $_COOKIE using  unset() function

unset($_COOKIE['user']);
 

To know more on cookies, visit: PHP Manual


Conclusion:

Hopes You got some Idea on How to use Cookies in PHP. Please comment if  any doubts or questions regarding Cookies.

Happy Learning.


Previous: Sessions


Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP