PHP Setcookie () usage, Phpsetcookie usage
Definition and usage
The Setcookie () function sends an HTTP cookie to the client.
A cookie is a variable that is sent to the browser by the server. A cookie is usually a small text file that the server embeds into a user's computer. This cookie is sent whenever the computer requests a page through a 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 value of the cookie.
There can be no loss before a cookie is assigned a value. If successful, the function returns True, otherwise false is returned.
Note:cookie settings must be refreshed later to take effect.
Grammar
- Setcookie(name,value,expire,path,domain,secure)
| Parameters |
Description |
| Name |
Necessary. Specifies the name of the cookie. |
| Value |
Necessary. Specifies the value of the cookie. |
| Expire |
Optional. Specify the validity period of the cookie. |
| Path |
Optional. Specifies the server path of the cookie. |
| Domain |
Optional. Specifies the domain name of the cookie. |
| Secure |
Optional. Specifies whether to transfer cookies through a secure HTTPS connection. |
Hints and Notes
Note: You can access the value of a COOKIE named "User" by $HTTP _cookie_vars["user"] or $_cookie["user".
Note: When a cookie is sent, the value of the cookie is automatically URL-encoded. The URL is decoded when it is received. If you don't need this, you can use Setrawcookie () instead.
Example 1
Set up and send cookies:
- !--? PHP
- $value = My cookie value "
- //send a simple cookie
- setcookie ( "TestCookie" $value
- ?> ...
- Php
- $value = "My cookie Value";
- Send a cookie that expires in the 24 child
- Setcookie("TestCookie",$value, time() +3600*);
- ?> ...
Example 2
Different ways to retrieve cookie values:
- !--? PHP
- //output individual cookies
- echo $_cookie[ "TestCookie" ;
- echo "
"
- echo $HTTP _cookie_vars[ "TestCookie"
- echo "
" ;
- //output all Cookies
- print_r ($_cookie
- ?>
Output:
- My cookie value
- My cookie value
- Array ([testcookie] = my cookie value)
Example 3
Delete a cookie by setting the expiration date to the last Date/time:
- Php
- Set the expiration date to one hour ago
- Setcookie ("TestCookie", " ", time() - 3600);
- ?> ...
Example 4
Create an array cookie:
- Php
- Setcookie("Cookie[three]", "cookiethree");
- Setcookie("cookie[two]", "cookietwo");
- Setcookie("Cookie[one]", "cookieone");
- Output cookie (after reloading the page)
- If (isset($_cookie["COOKIE"])) {
- foreach ($_cookie["COOKIE"] as $name + = $value) {
- Echo "$name: $value
";
- }
- }
- ?> ...
Output:
- Three : cookiethree
- Both : cookietwo
- One : cookieone
Example 5
An issue that does not take effect when a cookie is set. Usually the reason why the scope is not set
- Php
- Setcookie("A","BB", Time() +3600,"/",". hi-docs.com");
- Note that the domain name is set to its own
- ?>
http://www.bkjia.com/PHPjc/1048751.html www.bkjia.com true http://www.bkjia.com/PHPjc/1048751.html techarticle PHP Setcookie () usage, Phpsetcookie usage definitions and usage setcookie () function sends an HTTP cookie to the client. A cookie is a variable that is sent to the browser by the server. Cookies are usually ...