PHP Cookies
- Tuesday, June 29, 2010, 18:23
- PHP
- Add a comment
|
|
A cookie is used to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, we are going to create and retrieve cookie values.
In PHP, setcookie () function is used to set a cookie.
Function: setcookie ()
setcookie(name, value, expire);
When we create a cookie, using the function setcookie, We must specify the following arguments.
- name: The name of your cookie. This name will use later to retrieve your cookie
- value: The value that is stored in your cookie.
- expiration: The date when the cookie will expire and be deleted.
Note: The setcookie() function must appear before <html> tag.
Creating a cookie:
{code codetype=php}<?php
setcookie(“user”, “Username”, time()+3600);
?>
<html>{/code}
Retrieving a cookie:
{code codetype=php}<?php
echo $_COOKIE["user"];
print_r($_COOKIE);
?>{/code}
Deleting a cookie:
{code codetype=php}<?php
// set the expiration date to one hour ago
setcookie(“user”, “”, time()-3600);
?>{/code}
Reference: w3schools.com


