The setcookie() function sends an HTTP cookie to the client.
A cookie is a variable sent to the browser by the server. Cookies are typically small text files that a server embeds on a user's computer. This cookie is sent each time the same computer requests a page through the browser.
The name of the cookie is automatically assigned to a variable of the same name. For example, if the cookie being sent is named "user", a variable named $user will automatically be created containing the cookie's value.
The cookie must be assigned before any other output is sent to the client.
If successful, the function returns TRUE. Returns FALSE on failure.
setcookie(name,value,expire,path,domain,secure)
parameter | describe |
---|---|
name | Required. Specifies the name of the cookie. |
value | Required. Specifies the cookie value. |
expire | Optional. Specifies the cookie expiration time. time()+3600*24*30 will set the cookie expiration time to 30 days. If this parameter is not set, the cookie will automatically expire after the session ends (that is, when the browser is closed). |
path | Optional. Specifies the server path for cookies. If the path is set to "/", the cookie will be valid within the entire domain name. If the path is set to "/test/", the cookie will be valid under the test directory and all its subdirectories. The default path value is the current directory where the cookie is located. |
domain | Optional. Specifies the domain name of the cookie. In order for the cookie to be valid in all subdomains of example.com, you need to set the domain name of the cookie to ".example.com". When you set the domain name of the cookie to www.example.com, the cookie is only valid in the www subdomain. |
secure | Optional. Specifies whether cookies need to be transmitted over a secure HTTPS connection. Set to TRUE if cookies need to be transmitted over a secure HTTPS connection. The default is FALSE. |
Tip: The value of the cookie named "user" can be accessed via $HTTP_COOKIE_VARS["user"] or $_COOKIE["user"].
Note: When sending a cookie, the cookie value is automatically URL-encoded. URL decoding is performed automatically upon reception. If you don't need this, you can use setrawcookie() instead.
Set and send cookies:
<?php$value = "my cookie value";// send a simple cookiesetcookie("TestCookie",$value);?><html><body>......
<?php$value = "my cookie value";// send a cookie that expires in 24 hourssetcookie("TestCookie",$value, time()+3600*24);?><html><body>... ...
Different ways to retrieve the cookie value (after the cookie is set):
<html><body><?php// Print individual cookiesecho $_COOKIE["TestCookie"];echo "<br />";echo $HTTP_COOKIE_VARS["TestCookie"];echo "<br />";// Print all cookiesprint_r($_COOKIE);?></body></html>
The above code will output:
my cookie valuemy cookie valueArray ([TestCookie] => my cookie value)
Delete a cookie by setting the expiration date to a date/time in the past:
<?php// Set the expiration date to one hour agosetcookie ("TestCookie", "", time() - 3600);?><html><body>......
Create an array of cookies:
<?phpsetcookie("cookie[three]","cookiethree");setcookie("cookie[two]","cookietwo");setcookie("cookie[one]","cookieone");// print cookies (after reloading page)if (isset($_COOKIE["cookie"])) { foreach ($_COOKIE["cookie"] as $name => $value) { echo "$name : $value <br />"; } }?><html><body>......
The above code will output:
three : cookiethreetwo : cookietwoone : cookieone