Definition and usage
The setcookie() function sends an HTTP cookie to the client.
A cookie is a variable sent by the server to the browser. Cookies are typically small text files that a server embeds on a user's computer. This cookie is sent each time the computer requests a page through the browser.
The name of the cookie is specified as a variable of the same name. For example, if the cookie being sent is named "name", a variable named $user is automatically created containing the cookie's value.
There cannot be any losses before assigning a value to the cookie. The function returns true if successful, false otherwise.
Note: Cookie settings must be refreshed before they can take effect.
Syntax
setcookie(name,value,expire,path,domain,secure)
Parameters | Description |
---|---|
name | required. Specifies the name of the cookie. |
value | Required. Specifies the value of the cookie. |
expire | Optional. Specifies the validity period of the cookie. |
path | optional. Specifies the server path for cookies. |
domain | Optional. Specifies the domain name for the cookie. |
secure | Optional. Specifies whether cookies are transmitted over a secure HTTPS connection. |
Tips and Notes
Note: The value of the cookie named "user" can be accessed through $HTTP_COOKIE_VARS["user"] or $_COOKIE["user"].
Note: When sending a cookie, the cookie value is automatically URL encoded. URL decoding is done on reception. If you don't need this, you can use setrawcookie() instead.
Example 1
Set and send cookie:
<?php $value ="my cookie value"; // 发送一个简单的 cookie setcookie("TestCookie",$value); ?> <?php $value ="my cookie value"; // 发送一个 24 小时候过期的 cookie setcookie("TestCookie",$value, time()+3600*24); ?>
Example 2
Different ways to retrieve cookie value:
// 输出个别的 cookie echo $_COOKIE["TestCookie"]; echo " "; echo $HTTP_COOKIE_VARS["TestCookie"]; echo " "; // 输出所有 cookie print_r($_COOKIE); ?>
Output:
my cookie value my cookie value Array([TestCookie]=>my cookie value)
Example 3
Passed Set the expiration date to a date/time in the past, delete a cookie:
<?php // 把失效日期设置为一小时前 setcookie ("TestCookie","", time()-3600); ?>
Example 4
Create an array of cookies:
<?php setcookie("cookie[three]","cookiethree"); setcookie("cookie[two]","cookietwo"); setcookie("cookie[one]","cookieone"); // 输出 cookie (在重载页面后) if(isset($_COOKIE["cookie"])){ foreach($_COOKIE["cookie"]as $name => $value){ echo "$name : $value "; } } ?>
Output:
three : cookiethree two : cookietwo one : cookieone
Example 5
About setting cookies The problem does not take effect after. Usually the reason is that the scope is not set
<?php setcookie("a","bb",time()+3600,"/",".hi-docs.com"); // 注意把域名设置为自己的 ?>
The above introduces the usage of PHP setcookie, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.
Related articles:
If setcookie does not set the expiration time, how should I write the set path?
php setcookie function invalid
cookies setting PHP setcookie setting Cookie usage and setting invalid problem