The relationship between cookies and sessions
JS --- cookie and session
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.
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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Session failure is usually caused by the session lifetime expiration or server shutdown. The solutions: 1. Extend the lifetime of the session; 2. Use persistent storage; 3. Use cookies; 4. Update the session asynchronously; 5. Use session management middleware.

Solution to the cross-domain problem of PHPSession In the development of front-end and back-end separation, cross-domain requests have become the norm. When dealing with cross-domain issues, we usually involve the use and management of sessions. However, due to browser origin policy restrictions, sessions cannot be shared by default across domains. In order to solve this problem, we need to use some techniques and methods to achieve cross-domain sharing of sessions. 1. The most common use of cookies to share sessions across domains

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

More and more users are starting to upgrade the win11 system. Since each user has different usage habits, many users are still using the ie11 browser. So what should I do if the win11 system cannot use the ie browser? Does windows11 still support ie11? Let’s take a look at the solution. Solution to the problem that win11 cannot use the ie11 browser 1. First, right-click the start menu and select "Command Prompt (Administrator)" to open it. 2. After opening, directly enter "Netshwinsockreset" and press Enter to confirm. 3. After confirmation, enter "netshadvfirewallreset&rdqu

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.

Recently, many win10 users have found that their IE browser always automatically jumps to the edge browser when using computer browsers. So how to turn off the automatic jump to edge when opening IE in win10? Let this site carefully introduce to users how to automatically jump to edge and close when opening IE in win10. 1. We log in to the edge browser, click... in the upper right corner, and look for the drop-down settings option. 2. After we enter the settings, click Default Browser in the left column. 3. Finally, in the compatibility, we check the box to not allow the website to be reloaded in IE mode and restart the IE browser.
