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.
- 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
Use of PHP Cookies
- 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.
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
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.
setcookie(name, value, expire, path, domain, secure, httponly);
Here is the Detail of All the Arguments
|Name||This parameter is used to set the name of the Cookie. Same name will be used later to access that PHP cookies information in |
|Value||This parameter will set the value of the Cookie. This is the content or information that you actually want to store.|
|Expiry||This 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.|
|Path||This specifies the directories for which the cookie is valid. A single forward slash (/) character allows the cookie to be valid for all directories.|
|Domain||This can be used to specify the domain name. All cookies are only valid for the host and domain which created them.|
|Security||This 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
<?php setcookie("mobile", "914343434", time() + (86400 * 30), "/"); // 86400 = 1 day echo "Cookie '" . $_COOKIE["mobile"] . "' is set!\n"; ?>
Run : http://ideone.com/XlN3Vg
Modify a Cookie Value
To modify a cookie, just set (again) the cookie using the
<?php setcookie("mobile", "17635673", time() + (86400 * 30), "/"); // 86400 = 1 day echo "Cookie '" . $_COOKIE["mobile"] . "' is set!\n"; ?>
Run : http://ideone.com/3ftj9R
Delete a Cookie
To delete a cookie, use the
setcookie() function with an expiration date in the past.
<?php 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 : http://ideone.com/Slzpfj
|Exercises & Assignments|
|No Content Found.|
|Interview Questions & Answers|
|Difference Between Cookies and Session|