search
HomeWeb Front-endJS TutorialHow to distinguish Cookies and Session? The difference and detailed explanation between Cookies and Session in JavaScript

This article mainly introduces the detailed explanation and difference between Cookies and Session. Friends in need can refer to the following

The detailed explanation and difference between Cookies and Session

1.Cookie is a text string handle sent to the client's browser and stored on the client's hard drive. It can be used to persist data between sessions of a WEB site.

2.session actually refers to the period of time from when a visitor arrives at a specific homepage to when he leaves. Session actually uses cookies for information processing. When the user first makes a request, the server creates a cookie on the user's browser. When the session ends, it actually means that the cookie has expired.

Note: The name of the cookie created for this user is assessionid. The only purpose of this cookie is to provide different identity authentication for each user.
session Literally speaking, it is a session. This is similar to when you are talking to someone. How do you know that the person you are talking to is Zhang San and not Li Si? The other party must have certain characteristics (such as appearance) that indicate that he is Zhang San. The same principle applies to sessions. The server needs to know who is currently sending the request to itself. In order to make this distinction, the server will assign a different "identity identifier" to each client. Then every time the client sends a request to the server, it will bring this "identity identifier", and the server will know that the request comes from Who. As for how the client saves this "identity", there are many ways. For browser clients, everyone uses cookies by default.

3. What cookies and sessions have in common is that both cookies and sessions are session methods used to track the identity of browser users.

4. The difference between cookie and session is: cookie data is stored on the client side, and session data is stored on the server side.

Simply put, when you log in to a website:

If the web server uses session, then all data is saved on the server.

Every time the client requests the server, it will send the sessionid of the current session. The server determines the corresponding user data flag
based on the current sessionid to determine whether the user is logged in or has certain permissions. Since the data is stored on the server, you cannot forge it,
But if you can obtain the sessionid of a logged-in user, you can also successfully forge the user's request using a special browser.
sessionid is randomly assigned when the server and client connect. Generally speaking, there will be no duplication, but if there are a large number of concurrent requests,
it is not without the possibility of duplication.
· If the browser If cookies are used, all data is stored on the browser side. For example, after you log in, the
server sets a cookie username. Then when you request the server again, the browser will send the username to the server. ,
These variables have certain special marks. The server will interpret it as a cookie variable, so as long as the browser is not closed, the cookie variable will always be valid,
so it can be guaranteed not to be disconnected for a long time. If you can intercept a user's cookie variable and then forge a data packet to send over,
then the server still thinks you are legitimate. Therefore, the possibility of being attacked using cookies is relatively high. If the validity time is set,
then it will save the cookie on the client's hard drive. The next time you visit the website, the browser will first check whether there is a cookie,
if there is one, read it The cookie is then sent to the server. If you save a forum cookie on your machine, the
validity period is one year. If someone invades your machine, copies your cookie, and then puts it in the directory of his browser,
then he When you log in to the website, you log in with your identity. So cookies can be forged. Of course, you need to pay attention when forging.
Directly copy the cookie file to the cookie directory. The browser will not recognize it. It has an index.dat file that stores the creation time of the cookie file,
and whether it has been modified. So you must first have the cookie file of the website, and you must deceive the browser from the effective time.

5. Both can be used to store private things. They also have validity periods. The difference is that the session is placed on the server. Whether it expires or not depends on the setting of the service period. Cookies are If it exists on the client side, whether it has been used or not can be set when the cookie is generated.

(1)Cookie data is stored on the client's browser, and session data is placed on the server
(2)Cookies are not very secure. Others can analyze the COOKIE stored locally and perform COOKIE Deception, if security is the main consideration, session
(3)session will be saved on the server for a certain period of time. When access increases, it will take up more of your server's performance. If the main consideration is to reduce server performance, you should use COOKIE
(4) The limit of a single cookie on the client is 3K, which means that a site cannot store COOKIE on the client. 3K.
(5)So: Store important information such as login information as SESSION; if other information needs to be retained, it can be placed in COOKIE

Students who need to learn js please pay attention to php Chinese websitejs video tutorial, many js online video tutorials can be watched for free!

The above is the detailed content of How to distinguish Cookies and Session? The difference and detailed explanation between Cookies and Session in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor