How to set cookie in PHP
Cookie is a piece of information stored in client machine. PHP use
the function setcookie() to set or delete cookie
in the client machine.
Syntax:
bool setcookie ( string cookiename [,
string value [, int
expire [, string path [,
string domain [, bool secure]]]]]
)
setcookie() parameters
| Parameter |
Description |
Examples |
| cookiename |
The name of the cookie. |
'cookiename' is called as $_COOKIE['cookiename'] |
| value |
The value of the cookie. This value is stored on the clients
computer |
this value is retrieved through $_COOKIE['cookiename'] |
| expire |
The time the cookie expires. |
time()+60*60*24*30 will set
the cookie to expire in 30 days. If not set, the cookie will
expire at the end of the session (when the browser closes). |
| path |
The path on the server in which the cookie will be available
on. |
If set to '/', the cookie will
be available within the entire domain.
If set to '/foo/', the cookie
will only be available within the /foo/ directory
and all sub-directories such as /foo/bar/ of domain.
The default value is the current directory that the cookie
is being set in. |
| domain |
The domain that the cookie is available. |
To make the cookie available on all subdomains of example.com
then you'd set it to '.example.com'.
The . is not required but makes
it compatible with more browsers. Setting it to www.example.com will
make the cookie only available in the www subdomain.
Refer to tail matching in the spec for
details. |
| secure |
Indicates that the cookie should only be transmitted over
a secure HTTPS connection. When set to TRUE,
the cookie will only be set if a secure connection exists.
The default is FALSE. |
0 or 1 |
In all the parameters showing above, except cookiename, could be
omitted.
PHP set cookie
The most common used setcookie function is following:
setcookie("kidslovepc","1",time()+7*24*60*60);
//expire after 7 days.
The other options are shown in following examples:
setcookie("kidslovepc");//cookie
expire once after client closes all browsers
//cookie will expire after one hour
//cookie only accessible to directory php-tutorial and all its subdirectory
setcookie("kidslovepc","1",time()+60*60,"/php-tutorial");
//cookie accessible to all directory,"/"
//cookie is session cookie, time is 0
//cookie accessible to all subdomains of kidslovepc.com
setcookie("kidslovepc","1",0,"/","kidslovepc.com");
Retrieve Cookie
Once the cookies have been set, they can be accessed with
the $_COOKIE or $HTTP_COOKIE_VARS arrays.
<?php
// Print an individual cookie
echo $_COOKIE["kidslovepc"];
echo $HTTP_COOKIE_VARS["kidslovepc"];
// view all cookies
print_r($_COOKIE);
?>
Delete cookie
We also your setcookie() function to delete cookie. When deleting
a cookie, the expiration date is set in the past, to trigger the
removal mechanism in browser.
Examples follow how to delete
cookies:
<?php
// set the expiration date to one hour ago
setcookie ("kidslovepc", "", time()
- 60);
setcookie ("kidslovepc", "", 0, "/", ".kidslovepc.com");
?>
Cookie Array
Array is a good way to set multiple cookie in client machine.
<?php
// set the cookies
setcookie("kidslovepc[three]", "cookiethree");
setcookie("kidslovepc[two]", "cookietwo");
setcookie("kidslovepc[one]", "cookieone");
// after the page reloads, print them out
if (isset($_COOKIE['kidslovepc'])) {
foreach ($_COOKIE['kidslovepc'] as $name => $value) {
echo "$name : $value <br />\n";
}
}
?>
Delete cookie Array
<?php
foreach
($_COOKIE as $key=>$value)
{
setcookie ($key, $value, time()
- 1);
}
?>
|