Cookie & amp; session, cookies

Source: Internet
Author: User
Tags php session

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

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.