


In PHP, what is the difference between cookies and sessions, and what are the benefits of using them? (Please give me the answer, not Baidu, thank you)?
Reply content:
Let’s not talk about sessions and cookies in Php. Let’s take a look at what session and cookies are in a normal web.Cookie is actually a technology on the browser side. Since HTTP itself is stateless, generally speaking, the server does not know who is requesting each time. Therefore, a web session allows temporary storage of some content on the client through set-cookie in the HTTP header. The client needs Each HTTP request in this session is accompanied by the contents of the current cookie (it will not be sent if cookies are disabled). Sessions here are generally defined by domain names.
But cookies have two big flaws: 1. Cookies are stored on the client side, so cookies can be read, misappropriated and modified. This means that if any critical information, such as login information, is stored using cookies, others can read, delete, and modify it at will. 2. Other disguised sites may be able to directly obtain your cookie content.
So how to access this information so that it is difficult for the client to delete cookies at will and disguise other users? Generally speaking, there are two major categories of methods: 1. Encrypt the cookie; 2. Only store a signature with self-verification in the cookie, and then store the key data on the server side. This is the session.
session is the application of cookies. Encrypting sessions stored on the client is the easiest to implement and does not bring additional storage burden to the server. Sessions of many new micro-frameworks are implemented in this way (and are not stored on the server). The second is to store it on the server. Optional storage locations include but are not limited to memory, files, external databases, etc.
Php comes with its own library. Cookie is to add cookies that need to be stored on the client side to the response. Session is to add a session_id to the cookie, and then store the things that need to be stored on the server side in the form of files by default.
By the way, modern browsers generally support localStorage. Firstly, it is accessed through the front end but not available from the backend. It is slightly safer for users. Secondly, the amount of storage can be much larger. Thirdly, there is a certain degree of prevention. The capabilities of low-end crawlers. If you do not need to consider compatibility with old browsers, it is more recommended to use localStorage + front-end and back-end separation, and return localStorage content (session_id or encrypted storage content) through js in the form of ajax to replace cookies. Cookie is to store data on the client side. Session is to store data on the server side. The client only stores one ID. The first floor said it very well.
In fact, the biggest difference is that the cookie exists on the client side and the session exists on the server side. And generally speaking, the session does depend on the session id in the cookie. In fact, you can also set the session id yourself, and then specify the session id when using the session. The function of session id is to specify the file stored in the session, so you can use this trick after cookies are disabled.
Another point to add is that sessions should be used with caution in a distributed architecture, because session data may be lost. Of course, you can also rewrite the session so that the session is stored in the cache or database. You can understand it this way, the cookie is someone else's box, and the session is your own box. What are the benefits of the box? Save something

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



The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Alipay PHP...

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
