PHP, the difference between a cookie and a session, what are the benefits of using it? (ask you to answer, do not Baidu, thank you)?

Source: Internet
Author: User

Reply content:

Instead of talking about the session and cookie in PHP, let's look at what the session and cookie are in the normal web.

Cookies are actually technology at the end of the browser. Since HTTP itself is stateless, it is generally not known to the server side who each request is, so a Web session allows temporary storage of some content in the HTTP header via Set-cookie on the client, The client needs to enclose the contents of the current cookie for each HTTP request in this session (the cookie is disabled). The conversation here is usually defined by a domain name.

But there are two big drawbacks to cookies: 1. Cookies are stored on the client, so cookies can be read, misappropriated, and modified. This means that for any key information, such as login information, if it is kept with a cookie, others will be able to read and revise it. 2. Other masquerading sites may have access to your cookie content directly.

So how do you access this information to make it difficult for the client to arbitrarily hack cookies to disguise other users? Generally, there are two main classes of methods: 1. Encrypt the cookie, save only one signature with self-authentication in 2.cookie, and then store the key data at the server end. This is the session.

Session is the application of cookies. Encrypted storage in the client session is the easiest to implement, does not bring additional storage burden on the server, many of the new small micro-framework of the session is implemented by this means (and not stored on the service side). The second is the storage on the server side, the optional storage includes not limited to memory, file, external database and so on.

PHP comes with a library, the cookie is added to the response, such as the need for client storage, such as Cookie,session is in a cookie session_id, and then by default in the form of files to store storage on the server side.

Incidentally, the modern browser generally support localstorage, one of it through the front-end access to the backend is not available to users, the security is slightly better, and the amount of storage can be mostly, Sunline has a certain ability to prevent low-end crawler. If you do not consider the old browser compatibility, with the localstorage+ front-end separation, in the form of Ajax through JS return localstorage content (session_id or encrypted storage content) in lieu of cookies more recommendable. A cookie is the presence of data on the client
Session is to put the data on the server side, the client only save one ID first floor said very well.
In fact, the biggest difference is that the cookie exists on the client side, the session server. And in general, the session really depends on the session ID in the cookie, in fact, the session ID you can also decide, and then use the session to specify the session ID. The role of session ID is to specify the session file, so the cookie is disabled, you can use this trick.
Add that, in the distributed architecture, the session should be used with caution, because the session data loss may occur, of course, you can also rewrite the session, the session exists in the cache or in the database. You can understand that the cookie is someone's home box, the session is the box of their own home, the advantages of the box? Save something.
  • 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.