Home Backend Development PHP Problem What is the difference between session and cookie in php?

What is the difference between session and cookie in php?

Sep 05, 2019 pm 04:51 PM
cookie php session the difference

What is the difference between session and cookie in php?

1. Session

(1) The origin and introduction of Session

Session: in In computers, especially in network applications, it is called "session control". The survival time is the time that elapses from entering the website to closing the website when the user browses the website, that is, the time it takes the user to browse the website. time.

Since Http is a stateless protocol, it is only responsible for requesting the server. When it responds to the server, it loses contact with the browser. The user's personal information cannot be saved, just like the relationship between a shopping mall and a vending machine or ordinary people, so in order to make up for this shortcoming, Session came out, and Session needs to be version 4.1 or above.

php Chinese website learning topic: php session (including pictures, texts, videos, cases)

(2) Session mechanism

The session mechanism adopts a solution to maintain state on the server side, and has its own corresponding unique identifier session_id, which distinguishes the previous information of different users

For example: when we go to a restaurant to eat, we have a membership card with my personal information and validity period. The restaurant can find my personal information every time I come.

( 3) How session works

1>When a session is enabled for the first time, a unique identifier is stored in a local cookie.

2>First use the session_start() function, and PHP loads the stored session variables from the session warehouse.

3>When executing a PHP script, register the session variable by using the session_register() function.

4>When the PHP script execution ends, the session variables that have not been destroyed will be automatically saved in the local session library under a certain path. This path can be specified by session.save_path in the php.ini file. You can load it and use it the next time you browse the web.

Saved files:

What is the difference between session and cookie in php?

(4) What are the practical applications of Session projects

1> Record user login information, use global

2> to create verification codes, and verify comparison operations

3> Saving sessions to the database can achieve: a. Controlling that only one person can log in to an account

(5) Advantages and Disadvantages of Session

1> Advantages of session: uniqueness, convenient calling, not occupying too many resources, it is a group of temporary files stored on the server data. Under normal circumstances, when we log in as a user, we will store the user data in the session. In this way, it can be easily called on any page, and each client will generate a unique session_id, so there will be no confusion. And after closing the browser, the server will have a session recycling mechanism to automatically delete expired sessions

2> Disadvantages: It is saved in the form of cookies on the client, and it is useless if cookies are disabled.

(6) The connection and difference between Session and Cookie

1>Contact: Session also needs to save an identity on the client side, so it is necessary to use Cookie. Session is The session and cookie that work through cookies are connected through $_COOKIE['PHPSESSID']. Through $_COOKIE['PHPSESSID'], you can know the session ID and obtain other information.

2> Difference: The cookie mechanism uses a solution that maintains state on the client (browser), while the session mechanism uses a solution that maintains state on the server side

( 7) Use of Session

sesstion_start(); // First open the session

$_SESSION['user'] = 'username'; // Store username in it

echo $_SESSION['user']; // Directly output username

session_destroy() ; // Destroy session

2. Cookie

(1) Origin and introduction of Cookie

Cookie is used as the Internet The product is generated to save some basic information of the user. It can also be understood as some information temporarily saved by the server on your computer. Next time you come, take a look at this information and then load it directly. Send specific information.

(2) Cookie mechanism

The mechanism of Cookie and Session are similar. They will generate a specific value for each user. This time it is in the client Saved on the end, when we open the website that saves our personal information in the cookie, it will submit our personal information to the server, and then request the corresponding information from the server

(3) Principle of Cookie

When you visit a website, you can save a small amount of data from the server to the hard disk, such as user name, id, etc. When you visit next time, the server will first look for this file and then read it. Relevant information, so that you can log in to the website directly without entering your username and password

(4) What are the applications of cookies in actual project

1>Remember username and password

(5) Advantages and disadvantages of Cookie

1>Advantages: a. No server resource space required b. Longer persistence

2>Disadvantages: a. The client size is limited b. If the user disables the cookie, it will be useless c. Unsafe

(6) The difference and connection between Cookie and Session

1>Contact: Cookie can save a unique identifier for the session on the client side

2>Difference: The Cookie mechanism uses a solution that maintains state on the client side (browser), while The session mechanism adopts a solution to maintain state on the server side

(7) Use of Cookie

1> Creation and update: setCookie($cookieName,$value, time() seconds);

Example: setcookie("UserName","imooc",time() 2*7*24*3600);

Value: $uName=$ _COOKIE['UserName'];

2>Delete Cookie

setcookie($cookieName,value,time()-秒数);
Copy after login

//or

setcookie($cookiename, '');
Copy after login
Copy after login

//or

setcookie($cookiename, '');
Copy after login
Copy after login

Instance:

setcookie("UserName","imooc",time()-3600);
Copy after login

The above content is for reference only.

For more related questions, please visit the PHP Chinese website: PHP Video Tutorial

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

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles