What is the difference between cookie and session? (detailed introduction)

不言
Release: 2023-04-04 18:24:01
forward
3865 people have browsed it

This article brings you what is the difference between cookies and sessions? (detailed introduction) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Session is another mechanism for recording client status. The difference is that Cookie is saved in the client browser, while Session is saved on the server. When the client browser accesses the server, the server records the client information on the server in some form. This is Session. When the client browser visits again, it only needs to find the customer's status from the Session.

If the Cookie mechanism determines the customer's identity by checking the "passport" on the customer, then the Session mechanism confirms the customer's identity by checking the "Customer Details" on the server. Session is equivalent to a customer file created by the program on the server. When a customer comes to visit, he only needs to query the customer file table.

Session principle in plain language: When you access the server once, the server will open up a space in the memory, return the only key to open the space, and then return the key to the browser. When you visit for the second time, the browser will carry this key to the server to open the corresponding space. If the space has been destroyed, it will open up a new space and return the new key to the browser.

DIFFERENCE

cookie

session

Definition

The website stores the user in order to identify the user’s identity Data on the local terminal (usually encrypted)

Data saved by the server to track user status

Different access methods

can only save ASCII characters and cannot directly access objects

can access any data type

## Different privacy policies

are stored on the client, visible to users, and can be modified and copied

Stored on the server, transparent to users and more secure

The validity period is different

You can set it to be valid for a long time, just set a particularly large expiration time

Shorter validity period

Browser Supporting different

requires client browser support. If the browser disables or does not support cookies, you need to use session and URL address rewriting (append the session id directly to the end of the URL path), which can be set to be valid in all windows or valid in the current window and sub-windows

Only valid within this browser window and sub-window

Cross-domain support for different

Support cross-domain name access

Does not support cross-domain name access, the session is only valid within the domain name where it is located

The above is the detailed content of What is the difference between cookie and session? (detailed introduction). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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