Php development cookie
Cookie is a mechanism for storing data remotely in a browser and tracking and identifying users. Simply put, cookies are a folder temporarily stored on the user's hard disk by the web server and can be read by the web browser at any time. When a user accesses the web site again, the website obtains cookies to record the user's specific access information (such as the last visit location, time spent, user name and password ), in this way, users can quickly log on without entering a password. The format of the text file is as follows: the user name @ website address contains the following functions:
1. Record some information of the visitor. For example, you can use cookies to record the number of times a user accesses a page or the information that the user has input. In addition, some websites can automatically record the user names that can be logged on last time.
2. pass variables between pages. The browser does not save any variable information on the current page. When the page is closed, all variable information on the page disappears. If you declare a variable id = 8 and want to pass the variable to another page, you can save the variable as a cookie first, then, read the value of the variable in the cookie on the next page.
3. Store the Internet page you want to view in the temporary cookie folder, which can speed up browser access in the future.
Use the setcookie () function in php to create a cookie. Cookie is a part of the HTTP header, and the header must be sent before other content on the page. It must be output first. If an HTML Tag, echo statement, or even a blank line are output before the setcookie () function, a program error may occur.
The syntax format is as follows:
Bool setcookie (string name [, string value [, int explre [, string path [, string domain [, int secure])
The parameters of the setcookie () function are described as follows:
The sample code for using the setcookie () function to create a cookie is as follows:
Cookie reading
In php, you can directly use the Global Array $ _ COOKIE [] to read the cookie value of the browser.
Use the print_r () function to read the cookie variable. The sample code is as follows:
The running result is as follows:
Delete cookie
To delete a cookie, you can use the setcookie function to reduce the cookie expiration time by one second. The following is an example:
Setcookie ("" visittime), time ()-1 );
Cookie Lifecycle
If no time is set for a cookie, it indicates that its lifecycle is the period of the browser session. If the browser is closed, the cookie will be deleted. This cookie is called a session cookie. It is generally stored in the memory instead of on the hard disk.
If the cookie expires, the browser saves the cookie to the hard disk. When the browser is opened again, the cookie still smiles until the cookie expires.
The Browser allows a maximum of 300 cookie files to be stored, and each cookie file supports a maximum capacity of 4 kb, and each domain name supports a maximum of 20 cookies. When the limit is reached, the browser will randomly Delete cookies.