In php, cookies are commonly used, but many friends may not know how cookies work. The following section will help you sort out the working principles of php cookies and
Cookies are commonly used in php, but many friends may not know how cookies work, the following small series will help you sort out the working principles of php cookies and some examples of cookie read/write operations.
Cookie and session status
For BS development, these two concepts are essential. First, let's take a rough look. it is difficult to go deep without practical applications. For more information, see the reference address!
What is Cookie?
Cookie is a short text message that is transmitted between the Web server and the browser as user requests and pages. Each time a user accesses a site, the Web application can read the information contained in the Cookie. Cookie basic working principle if the user visits the page on the site again, when the user enters URLwww. *****. com, the browser will find the Cookie associated with the URL on the local hard disk. If the Cookie exists, the browser sends it along with the page request to your site.
What are the functions of cookies?
The most fundamental purpose is that cookies can help websites save information about visitors. In a more general sense, Cookie is a way to maintain the continuity of Web applications (that is, to execute "state management"), so that the Web site remembers you.
• 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: users andSettings user name in the 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 Cookies corresponding to the website under the Cookies Directory of the C: DocumentsandSettings user name 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 use $ _ COOKIE ['XXX'] to read data.
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.
1. the instance code is as follows:
$ Value = "the best way is by yourself ";
Setcookie ("cookiename", $ value, time () + 60*60*24*7 );
Receive and process cookies
PHP has good support for cookies. Like form forms, PHP automatically receives HTTP headers from the web server when receiving them. Similar to form receiving, $ _ COOKIE ["cookiename"] or $ HTTP_COOKIE_VARS ["cookiename"] is used for receiving (not recommended)
Note: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:
First, the code is as follows:
Setcookie ("CookieArray [0]", "Value1 ");
Setcookie ("CookieArray [1]", "Value2 ");
Second, the code is as follows:
Setcookie ("CookieArray ['one']", "Value1 ");
Setcookie ("CookieArray ['two']", "Value2 ");
Use arrays in setcookie,The code is as follows:
-
- 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. there are two ways to delete an existing 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 period is the same, so the Cookie is deleted after the page is viewed (in fact, it is invalid ).
For example, the code is as follows:
Setcookie ("MyCookie", "Value", time ()-1); // delete 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. there cannot be any html output before setcookie (), 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. cookie processing is different without a browser. 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 such vulnerability.