PHPsetcookie () usage, phpsetcookie usage _ PHP Tutorial

Source: Internet
Author: User
Tags http cookie send cookies
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
  1. 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:

  1. $ Value = "my cookie value ";
  2. // Send a simple cookie
  3. Setcookie ("TestCookie", $ value );
  4. ?>......
  1. $ Value = "my cookie value ";
  2. // Send a cookie that expires in 24 hours
  3. Setcookie ("TestCookie", $ value, time () + 3600*24 );
  4. ?>......
Example 2

Different methods for retrieving cookie values:

  1. // Output individual cookies
  2. Echo $ _ COOKIE ["TestCookie"];
  3. Echo"
    ";
  4. Echo $ HTTP_COOKIE_VARS ["TestCookie"];
  5. Echo"
    ";
  6. // Output all cookies
  7. Print_r ($ _ COOKIE );
  8. ?>

Output:

  1. My cookie value
  2. My cookie value
  3. Array ([TestCookie] => my cookie value)
Example 3

Delete a cookie by setting the expiration date to the past date/time:

  1. // Set the expiration date to one hour ago
  2. Setcookie ("TestCookie", "", time ()-3600 );
  3. ?>......
Example 4

Create an array cookie:

  1. Setcookie ("cookie [three]", "cookiethree ");
  2. Setcookie ("cookie [two]", "cookietwo ");
  3. Setcookie ("cookie [one]", "cookieone ");
  4. // Output cookie (after reloading the page)
  5. If (isset ($ _ COOKIE ["cookie"]) {
  6. Foreach ($ _ COOKIE ["cookie"] as $ name => $ value ){
  7. Echo "$ name: $ value
    ";
  8. }
  9. }
  10. ?>......

Output:

  1. Three: cookiethree
  2. Two: cookietwo
  3. 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.

  1. Setcookie ("a", "bb", time () + 3600, "/", ".hi-docs.com ");
  2. // Set the domain name to your own
  3. ?>

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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.