Setcookie (PHP3, PHP4, PHP5) setcookie -- send a cookie description boolsetcookie (stringname [, stringvalue [, intexpire [, stringpath [, stringdomain [, boolsecure]) setcookie () defines a cooki sent together with other HTTP headers
Setcookie (PHP 3, PHP 4, PHP 5) setcookie -- send a cookie description bool setcookie (string name [, string value [, int expire [, string path [, string domain [, bool secure]) setcookie () defines a cooki sent together with other HTTP headers
Setcookie(PHP3,PHP4,PHP5)
Setcookie-- Send a cookie
Description boolSetcookie(String name [, string value [, int expire [, string path [, string domain [, bool secure])
Setcookie()Define a cookie sent together with other HTTP headers. Like other headers, cookies must be output in any other part of the script.BeforeSetcookie()If there is any output before, this function will fail and returnFALSE. IfSetcookie()If the function runs successfullyTRUE. This does not indicate whether the user has accepted the cookie.
[Indent]Note:FromPHP4. You can use the output cache to output the content before calling this function. The cost is to cache all the output to the browser on the server until you run the following command to send them. Can be used in codeOb_start ()AndOb_end_flush ()To implement such a function, or by modifyingPhpThe output_buffering configuration option in. ini can be implemented by modifying the server configuration file.
[/Indent]
All other parameters except name are optional. You can use an empty string ("") To skip this parameter. Because the expire parameter is an integer, it cannot be dropped by a Null String. You can use zero (0. The following tableSetcookie()Every parameter is explained. Learn more about Netscape cookie specificationsSetcookie()For details about each parameter and how HTTP cookie works by reading RFC 2965.
Table 1.Setcookie()Parameter Details
[Tr] parameter description example [/tr]
| Name |
Cookie name. |
Use $ _ COOKIE ['cookiename'] to call a cookie named cookiename. |
| Value |
Cookie value. This value is stored on the client and cannot be used to save sensitive data. |
If the name is 'cookiename', you can use $ _ COOKIE ['cookiename'] to obtain its value. |
| Expire |
The time when the Cookie expires. This is a Unix timestamp, that is, the number of seconds from the Unix epoch. In other words, it is usually usedTime ()The function plus the number of seconds to set the cookie expiration time. Or useMktime (). |
Time () + 60*60*24*30 will set the cookie to expire after 30 days. If not set, the cookie will expire after the session ends (usually closed by the browser. |
| Path |
Valid Cookie Path on the server. |
If this parameter is set to '/', the cookie is valid throughout the domain. If it is set to '/foo /', the cookie is valid only in the/foo/directory under the domain and its subdirectories, for example,/foo/bar /. The default value is the current directory of the cookie. |
| Domain |
The valid Domain Name of the cookie. |
To make the cookie valid for all subdomains under the example.com domain name, set this parameter to '.example.com '. Although it is not necessary, it will be compatible with more browsers. If this parameter is set to www.example.com, it is valid only in the www subdomain. |
| Secure |
Indicates whether the cookie is transmitted only through a secure HTTPS connection. When setTRUEThe cookie is only set in secure connections. The default value isFALSE. |
0 or 1 |
After the cookie is set, you can get its value through $ _ COOKIE or $ HTTP_COOKIE_VARS array on other pages. Note that the $ _ COOKIE form of autoglobals appliesPHP4.1.0 or later. While $ HTTP_COOKIE_VARS starts fromPHPIt can be used from 3. The Cookie value is saved in the $ _ REQUEST array.
[Indent]Note:IfPHPIf register_globals is set to on, the cookie value will still be included in the variable. In the following example, $ TestCookie is registered, but the $ _ COOKIE array is recommended.
[/Indent]
Common Defects:
- Cookies do not take effect on the page on which they are set. to test whether a cookie is successfully set, you can access its value through another page before it expires. The expiration time is set by the expire parameter. You can simply use print_r ($ _ COOKIE); to debug existing cookies.
- The Cookie can be deleted only when the same parameter is set and used. If its value is an empty string, orFALSEAnd other parameters are the same as those of the previous call.SetcookieThe cookie with the specified name will be deleted on the remote client.
- Because the cookie value is setFALSEWill make the client try to delete this cookie, so save it on the cookieTRUEOrFALSEInstead of using a boolean value0To indicateFALSE, Use1To indicateTRUE
- You can set the cookie name to an array, but the values of each element in the array cookie will be exclusive to the user's system. Consider usingExplode ()The function uses multiple names and values to set a cookie. We do not recommendSerialize ()For this purpose, because it may cause a security vulnerability.
InPHP3, in the samePHPMultiple times in the scriptSetcookie()To set the cookie, It will be executed in reverse order. If you want to delete a cookie before inserting another cookie, you need to put the Insert before the deletion. FromPHPFour or more callsSetcookie()It is executed in order.
The following examples illustrate how to send cookies:
Example 1.Setcookie()Sending example
$ Value = 'something from somewhere ';
Setcookie("TestCookie", $ value ); Setcookie("TestCookie", $ value, time () + 3600);/* expire in 1 hour */ Setcookie("TestCookie", $ value, time () + 3600 ,"/~ Rasmus/",". utoronto. ca ", 1 ); |
|
Note that the portion of the value in the cookie will be automatically encoded with urlencode upon sending and automatically decoded upon receipt, and the value will be assigned to the cookie variable with the same name. If you do not want to do this and are usingPHP5, you can useSetrawcookie (). In the following simple example, we can get the cookie value just set:
Php // Output a separate cookie Echo $ _ COOKIE ["TestCookie "; Echo $ HTTP_COOKIE_VARS ["TestCookie ";
// Another debugging method is to output all cookies. Print_r ($ _ COOKIE ); ?> |
[/Font]
[Font = new]
To delete a cookie, you must ensure that its expiration time is in the past to trigger the deletion mechanism of the browser. The following example shows how to delete the cookie you just set:
Example 2.Setcookie()Delete example
// Set the expiration time to one hour ago Setcookie("TestCookie", "", time ()-3600 ); Setcookie("TestCookie", "", time ()-3600 ,"/~ Rasmus/",". utoronto. ca ", 1 ); |
|
You can also set an array cookie by using array symbols in the cookie name. You can set multiple cookies as array units. When the script extracts cookies, all values are placed in an array:
Example 3.Setcookie()Example of using arrays in
Php // Set cookie Setcookie("Cookie [three]", "cookiethree "); Setcookie("Cookie [two]", "cookietwo "); Setcookie("Cookie [one]", "cookieone ");
// Refresh the page and display it If (isset ($ _ COOKIE ['cookies ')){ Foreach ($ _ COOKIE ['cooker'] as $ name => $ value ){ Echo "$ name: $ value \ N "; } } ?> |
The above example will output:
| Three: cookiethreetwo: cookietwoone: cookieone |
|