Home > Web Front-end > JS Tutorial > The relationship between cookies and sessions

The relationship between cookies and sessions

一个新手
Release: 2017-09-11 10:04:19
Original
1410 people have browsed it

JS --- cookie and session

  1. Since the HTTP protocol is a stateless protocol, when the server needs to record the user's status, it needs to use some mechanism to identify the specific status. user, this mechanism is Session. In a typical scenario, such as a shopping cart, when you click the order button, since the HTTP protocol is stateless, you do not know which user operated it, so the server needs to create a specific session for the specific user. Session is used to identify this user and track the user so that we know how many books are in the shopping cart. This Session is saved on the server side and has a unique identifier. There are many ways to save Session on the server side, including memory, database, and files. Session transfer must also be considered when clustering. In large websites, there is usually a dedicated Session server cluster to save user sessions. Session information is stored in memory, and some caching services such as Memcached are used to store the session.

  2. session is stored on the server side, so how can the information on the client quickly match the server side? (Many times, when you log in to a website, the next time you log in You no longer need to enter your username and password, this is the role of cookies). Each time an HTTP request is made, the client will send corresponding cookie information to the server. In fact, most applications use cookies to implement session tracking. When a session is created for the first time, the server will tell the client in the HTTP protocol that a session ID needs to be recorded in the cookie. This will be recorded for each subsequent request. The session ID is sent to the server and I know who you are. Someone asked if the client's browser is disabled What about cookies? Generally, in this case, a technology called URL rewriting is used for session tracking. That is, for each HTTP interaction, a parameter such as sid=xxxxx will be appended to the URL, and the server will use this to identify the user.

  3. Cookies can actually be used in some user-friendly scenarios. Imagine that you have logged into a website once, and you don’t want to enter your account again when you log in next time. What should you do? This information can be written into the cookie. When visiting the website, the script of the website page can read this information and automatically fill in the user name for you, which can facilitate the user. This is also the origin of the cookie name, giving users a little sweetness.

Session is a data structure saved on the server side, used to track (identify) the user's status. This data can be saved in clusters, databases, and files;
Cookie is A mechanism for the client to save user information. It is used to record some user information. It is also a way to implement Session.

1, session is on the server side, cookie is on the client (browser)
2, session is stored in a file on the server by default (not memory)
3, session operation depends on session id, and the session id is stored in the cookie. That is to say, if the browser disables cookies, the session will also be invalid (but it can be achieved in other ways, such as passing session_id in the URL)
4, session can be placed It can be in a file, database, or memory.
5. Session is generally used for user verification. Therefore, the core of maintaining a session is the unique identifier of the client, that is, session id

The difference between cookie and session:

1. The cookie data is stored on the client's browser, and the session data is stored on the server.
2. Cookies are not very safe. Others can analyze the COOKIE stored locally and deceive COOKIE
Session should be used considering security.
3. The session will be saved on the server within a certain period of time. When access increases, it will take up more of your server's performance
Considering reducing server performance, COOKIE should be used.
4. The data saved by a single cookie cannot exceed 4K. Many browsers limit a site to save up to 20 cookies.
5. So personal suggestion:
Store important information such as login information as SESSION
If other information needs to be retained, it can be placed in COOKIE

The above is the detailed content of The relationship between cookies and sessions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template