PHP: setcookie() Function
The setcookie() function defines a cookie to be sent along with the rest of the HTTP headers. Due to RFC requirements cookies, like all headers, must be sent before any output from your script. This requires that any calls to this function must be placed prior to any output, including <html> and <head> tags, as well as any whitespace.
All arguments to the setcookie() function are optional, except for the name.
| Parameter | Description | Examples |
| name | The name of the cookie | ‘cookiename’ is called as $_COOKIE[cookiename] |
| value | The value of the cookie. This value is stored on the client’s computer. Storing sensitive information is not recommended | Assuming the name is ‘cookiename’, this value is retrieved through $_COOKIE['cookiename'] |
| expire | The time at which the cookie expires. This is a Unix timestamp, so it is in the number of seconds since the epoch. This will most likely be set with the time() function plus the number of seconds before you want it to expire. Another option would be to use mktime(). | time()+60*60*24*30 will set the cookie to expire in 30 days. If this value is not set, the cookie will expire at the end of the session (when the browser is closed) |
| path | The path on the server in which the cookie will be available. | If set to ‘/’, the cookie will be available within the entire domain. If set to ‘/cgi-bin’, the cookie will only be available within the /cgi-bin/ directory. 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 ton all subdomains of testing.com the cookie would be set to ‘.testing.com’. The . is not required but it makes it compatible with most browsers. Setting it to www.testing.com will make the cookie only available to the www subdomain. |
| secure | Indicates that the cookie should only be transmitted over a secure connection. When set to TRUE, the cookie will only be set when a secure connection exists. The default settings is FALSE | 0 or 1 |
Once the cookies have been set, they will become available on the next page load. This means they will not become visible until the next loading of a page that the cookie should be visible for. To see if a cookie exists, calling print_r($_COOKIE); will print any information set in the cookie (if available).
When a cookie is deleted, it must be deleted with the same parameters it is was set with. If all of the arguments match a previous call to setcookie(), then the cookie will be deleted from the remote client.
When setting values, do not use TRUE/FALSE. Using these boolean values can cause an error when trying to process the cookie. Setting a cookie with the value of FALSE will cause the browser to attempt to delete the cookie. Instead use 0 for FALSE and 1 for TRUE.
Examples:
1) setcookie() send example
<?php
$value = ’something from somewhere’;
setcookie(“TestCookie”, $value);
setcookie(“TestCookie”, $value, time()+3600); /* expire in 1 hour */
setcookie(“TestCookie”, $value, time()+3600, “/~rasmus/”, “.example.com”, 1);
?>
—————————————————————————
2) setcookie() delete example
<?php
// set the expiration date to one hour ago
setcookie (“TestCookie”, “”, time() - 3600);
setcookie (“TestCookie”, “”, time() - 3600, “/~rasmus/”, “.example.com”, 1);
?>
—————————————————————————
3) setcookie() and arrays
<?php
// set the cookies
setcookie(“cookie[three]“, “cookiethree”);
setcookie(“cookie[two]“, “cookietwo”);
setcookie(“cookie[one]“, “cookieone”);
// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
foreach ($_COOKIE['cookie'] as $name => $value) {
echo “$name : $value <br />n”;
}
}
?>

