Cookie & session, cookies
1. Introduction and difference of cookies and sessions
Session information is stored on the server, but session IDs are stored on client cookies. Of course, php session storage methods are diversified, in this way, even if the cookie is disabled, the Cookie can be tracked completely on the client. For example, IE firefox cannot be used when the client disables the cookie.
2. Cookie configuration and application
Setcookie (string name, string value, int expire, string path, string domain, int secure );
The name is the identifier of the cookie variable name. In php, You can reference the cookie variable with the same common variable name. Value is the initial value of the cookie variable. expire indicates the effective time of the cookie variable; path indicates the relevant path of the cookie variable; domain indicates the website of the cookie variable; secure is valid only during secure https transmission. For example: SetCookie ("Cookie", "cookievalue", time () + 3600, "/forum", ".php100.com", 1 );
3. Cookie configuration and application
Receiving and Processing cookies PHP supports Cookie receiving and processing very well and is completely automatic. It is as simple as the FORM variable principle. For example, if you set a Cookie named MyCookier, PHP will automatically analyze it from the HTTP header received by the WEB server and form a variable named $ myCookie, which is the same as a common variable, the value of this variable is the Cookie value. Arrays also apply. Another method is to reference the global variable $ HTTP_COOKIE_VARS array of PHP. Examples are as follows: (assuming these are all set in the previous page and still valid) echo $ MyCookie; echo $ CookieArray [0]; echo $ _ COOKIE ["MyCookie"]; echo $ HTTP_COOKIE_VARS ["MyCookie"];
4. Cookie configuration and application
To delete an existing Cookie, you can use either of the following methods: 1. SetCookie ("Cookie", ""); 2. SetCookie ("Cookie", "value ", time ()-1/time (); restrictions on the use of cookies 1. It must be set before the HTML file content is output; 2. Inconsistent processing of cookies by different browsers, and sometimes error results appear. 3. restrictions are imposed on the client. A browser can create a maximum of 30 cookies, each of which cannot exceed 4 kb. Each WEB site can set a maximum of 20 cookies.
5. Session configuration and application
Session_start (); // initialize the session. in the file header $ _ SESSION [name] = value; // configure Seeeionecho $ _ SESSION [name]; // use sessionisset ($ _ SESSION [name]); // judge unset ($ _ SESSION [name]); // Delete session_destroy (); // consume all sessions