PHP Cookies

Cookies are small piece of data stored on the client browser.

More about PHP Cookies:

  • Unlike normal variables, cookies data can be maintained for long time in browser, even after you close the browser.
  • Data stored in cookies can be access in other pages as well. Therefore, you can use cookies to transfer small amount of data between different pages in a website.
  • Cookies are stored on browser level. Therefore, when a cookie created on one browser, it can be accessed on the same browser only.
  • PHP provides setcookie() function to create or delete cookies. Data stored in Cookies can be accessed by $_COOKIE superglobal variable.

Use of PHP Cookies

  • Cookies can be used to store user information to track them later. For example: We use we use Cookies to remember login username & password in most websites.
  • Cookies are also used to store user preferences such as website language or color theme. For example: User can be asked to select a color theme for website layout & Then we can store that as a cookies data user preferences.
  • Most of the advertisement platforms (such as Google & Amazon) access user cookies data to show them advertisement as per their person choice or based on geographical area.
  • There are many websites that use cookies to send promotional alerts or popup on regular duration.

How Cookies works

When a user fetch web page with a browser, some cookies can be created and stored on that browser by PHP. Now each time when user request a page with the same browser, it will send the cookie too.

Therefore, based on cookies data available, some special information can be sent to user. This special information can be advertisement, user language or color preference or remembering user login and password.

Create a Cookie

PHP provides setcookie() function to set a cookie. This function can take upto six arguments. This function should be called before tag. One PHP setcookie() can store single piece of information. Therefore, you need to call same function multiple times to store multiple data.

Syntax: setcookie(name, value, expire, path, domain, secure, httponly);

Here is the Detail of All the Arguments

NameThis parameter is used to set the name of the Cookie. Same name will be used later to access that PHP cookies information in $_COOKIE[] variable.
ValueThis parameter will set the value of the Cookie. This is the content or information that you actually want to store.
ExpiryThis parameter will set the future time in seconds since 00:00:00 GMT on 1st Jan 1970. After Expiry time cookie data can not be accessed. If expiry parameter is not set, cookie will be expired when the Web Browser is closed.
PathThis specifies the directories for which the cookie is valid. A single forward slash (/) character allows the cookie to be valid for all directories.
DomainThis can be used to specify the domain name. All cookies are only valid for the host and domain which created them.
SecurityThis can be set to 1 to specify that the cookie should only be sent by secure transmission using HTTPS . If it is set to 0, cookie can be sent by regular HTTP.

Create a Cookie

setcookie("mobile", "914343434", time() + (86400 * 30), "/"); // 86400 = 1 day
echo "Cookie '" . $_COOKIE["mobile"] . "' is set!\n";
Note: Note: Please reload/refresh the page to see the value of the cookie in each program if you set and access cookie on the same page.

Run :

Modify a Cookie Value

To modify a cookie, just set (again) the cookie using the setcookie() function.

setcookie("mobile", "17635673", time() + (86400 * 30), "/"); // 86400 = 1 day
echo "Cookie '" . $_COOKIE["mobile"] . "' is set!\n";


Run :

Delete a Cookie

To delete a cookie, use the setcookie() function with an expiration date in the past.

setcookie("mobile", "17635673", time() + (86400 * 30), "/"); // 86400 = 1 day
echo "Cookie '" . $_COOKIE["mobile"] . "' is set!\n";
setcookie("mobile", "", time() - 3600);
echo "cookie is deleted";


Run :

Note: Note: Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE array. Therefore, if you echo $_COOKIE after setcookie() function, it will not display output until you reload that page.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
Difference Between Cookies and Session