How cookies work _ PHP Tutorial

Source: Internet
Author: User
Tags set cookie
Cookie working principle. When a customer accesses a PHP-based website, the setcookie () function can be used in PHP to generate a cookie. after processing, the system sends the cookie to the client and stores it in C: Documen.
  • When a customer accesses a PHP-based website, the setcookie () function can be used in PHP to generate a cookie. after processing, the system sends the cookie to the client and stores it in C: \ Documents andSettings \ Username \ Cookies directory.
  • Cookie is part of the HTTP header. Therefore, the setcookie () function must be called before any HTML content is sent to the browser. This restriction is the same as the header () function (for details about head () function, refer to it ).
  • When the customer visits the website again, the browser will automatically send the cookie corresponding to the site under the C: \ DocumentsandSettings \ User name \ Cookies directory to the server, the server automatically converts the cookie sent from the client into a PHP variable. In PHP5, the cookie sent from the client is converted to a global variable. You can read data through $ _ COOKIE ['XXX.

Define a cookie

  • Set cookie:
  • Syntax: boolsetcookie (stringname, [stringvalue, [int expire, [stringpath, [stringdomain, [int secure]);

This cookie function can have six attributes, commonly used with three parameters.

$ Value = "the best way is by yourself ";

Setcookie ("cookiename", $ value, time () + 60*60*24*7 );

PHP has good support for cookies. Like form forms, PHP automatically receives HTTP headers from the web server when receiving them. It is the same as form receiving.$ _ COOKIE ["cookiename"]Or $ HTTP_COOKIE_VARS ["cookiename"] (not recommended)

If the website has several different file directories that use cookies without paths, you can only access this cookie in the path where the files with cookies are set. If a path is specified, the path used to access the cookie is the specified path.

Create a cookie array:

Setcookie ("CookieArray [0]", "Value1 ");

Setcookie ("CookieArray [1]", "Value2 ");

Setcookie ("CookieArray ['one']", "Value1 ");

Setcookie ("CookieArray ['two']", "Value2 ");

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 ";

}

}

?>

Delete cookie

1. if you call a setcookie with only the name parameter, the cookie named this name will be deleted from the client;

Setcookie ("MyCookie"); // delete MyCookie

2. the expiration time is enough. The Cookie will be deleted after the page is viewed (in fact, it is invalid ).

For example:

Setcookie ("MyCookie", "Value", time ()-1 );

// Delete the MyCookie.

Note: When a Cookie is deleted, its value is still valid on the current page. If you want to set the cookie to expire after the browser is closed. You can directly set expiretime to 0, or do not set this value.

For example, setcookie ("name", "value", 0 ).

Cookie considerations

  • 1. before setcookie (), there cannot be any html output, that is, space, and blank lines. it must be set before the html file content output.
  • 2. after setcookie (), you call echo $ _ COOKIE ["name"] on the current page without output. You must refresh or go to the next page to view the cookie value.
  • 3. you do not need a browser to process cookies differently. The client can disable cookies, and the browser also limits the number of cookies. A browser can create a maximum of 300 cookies, each of which cannot exceed 4 kB. each WEB site can set a maximum of 20 cookies.
  • 4. cookies are stored on the client. if you disable cookies, your cookies will naturally become useless! Therefore, to avoid over-reliance on cookies, you must first think about the solution if the cookie is disabled, in case of any possible reasons.

The compute () function generates a cookie. after processing, the system sends the cookie to the client and stores it in C: \ Documen...

Related Article

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.