PHPsetcookie () and phpsetcookie. PHPsetcookie () usage. phpsetcookie uses the statutory and usage setcookie () functions to send an HTTPcookie to the client. Cookie is a variable sent from the server to the browser. PHP setcookie () and phpsetcookie
Definition and usage
The setcookie () function sends an HTTP cookie to the client.
Cookie is a variable sent from the server to the browser. Cookies are usually small text files embedded into users' computers by servers. This cookie is sent every time a computer requests a page through a browser.
The cookie name is a variable with the same name. For example, if the sent cookie is named "name", a variable named $ user is automatically created, containing the cookie value.
No loss is allowed before the cookie value is assigned. If the call succeeds, the function returns true; otherwise, the function returns false.
Note: the cookie setting must be refreshed before it takes effect.
Syntax
- Setcookie (name, value, expire, path, domain, secure)
Parameters |
Description |
Name |
Required. Specifies the cookie name. |
Value |
Required. Specifies the cookie value. |
Expire |
Optional. Specifies the cookie validity period. |
Path |
Optional. Specifies the server path of the cookie. |
Domain |
Optional. Specifies the domain name of the cookie. |
Secure |
Optional. Specifies whether to transmit cookies through secure HTTPS connections. |
Tips and comments
Note: You can use $ HTTP_COOKIE_VARS ["user"] or $ _ COOKIE ["user"] to access the cookie value named "user.
Note: When sending a cookie, the cookie value is automatically URL encoded. URL decoding is performed when receiving the message. If you do not need this, you can use setrawcookie () instead.
Example 1
Set and send cookies:
-
- $ Value = "my cookie value ";
- // Send a simple cookie
- Setcookie ("TestCookie", $ value );
- ?>......
-
- $ Value = "my cookie value ";
- // Send a cookie that expires in 24 hours
- Setcookie ("TestCookie", $ value, time () + 3600*24 );
- ?>......
Example 2
Different methods for retrieving cookie values:
-
- // 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 past date/time:
-
- // Set the expiration date to one hour ago
- Setcookie ("TestCookie", "", time ()-3600 );
- ?>......
Example 4
Create an array cookie:
-
- 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
- Two: cookietwo
- One: cookieone
Example 5
The problem that the cookie does not take effect after it is set. It is usually because no scope is set.
-
- Setcookie ("a", "bb", time () + 3600, "/", ".hi-docs.com ");
- // Set the domain name to your own
- ?>
Http://www.bkjia.com/PHPjc/1048751.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1048751.htmlTechArticlePHP setcookie () usage, phpsetcookie usage definition and usage the setcookie () function sends an HTTP cookie to the client. Cookie is a variable sent from the server to the browser. Cookie usually...