Table of Contents
Basic use
Basic principles
Advanced use
Commonly used operations:
Basic use
Basic Principle
Home Backend Development PHP Tutorial Detailed introduction to cookies and sessions (pictures and text)

Detailed introduction to cookies and sessions (pictures and text)

Feb 15, 2019 pm 01:25 PM
cookie session

This article brings you a detailed introduction (pictures and texts) about cookies and sessions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Cookie technology

Cookie is a browser-side technology that can save data on the browser! Cookie refers to the data saved on the browser!

PHP supports cookie technology! PHP can issue instructions to the browser to save data to the browser!

The browser is responsible for saving data, and php is responsible for controlling what data the browser saves! (php uses cookie technology on the browser)

The cookie data saved on the browser can carry the data and make a request to the server every time the browser requests the server. At this time The script on the server can get this data!

Basic use

Set cookie variables, add, change, delete

Use internal functions setcookie Complete

Form: setcookie(name, value)

Get cookie variable, read

Use predefined array variable:$_COOKIE

This variable stores all cookie data carried when requesting from the browser!

Each element is a cookie variable data! The subscript is the name, the value is the value!

Basic principles

Using firebug’s network tab:

What did you do when setting up?

In response, the server sends an instruction to add a cookie variable to the browser!

After receiving the instruction, the browser added a cookie data to the browser!

When obtained, it did What?

The browser will carry all cookies of the current site to the server upon request!

php will automatically obtain the cookie carried by the browser and form a $_COOKIE array, which can be used by user scripts!

Advanced use

1. Cookie data can only be string data!

2.setcookie function can add, modify and delete!

If it does not exist, add it; if it exists, modify it!

Delete, you can use the form of leaving the value blank!

3. The expiration period of cookie variables

There is a concept of validity period for cookie data:

Default, temporary cookies. It will be saved until the browser is closed!

At the same time, it is supported to add the third parameter of setcookie to modify the validity period of cookie variables. The validity period is expressed as a timestamp, indicating at which point in time it will expire!

php can obtain the current timestamp through the time() function, and extend the cookie time by increasing time() in increments!

At this time, the server will issue the following instructions to the browser:

Time is represented on the network using Greenwich Mean Time! That time without the concept of time zones! GMT

The browser knows the validity period of the cookie variable!

Commonly used operations:

1 .Cookie has the concept of a valid path

The cookie variable will only take effect in the current directory and its descendant directories!

Set under test/, you can access under test/sub/

Inversely:

That’s because of the cookie:

You can change the effective path of cookie data:

Do it through the fourth parameter of setcookie Modification:

/ indicates that the site root directory is valid! Valid for the entire site!

5. The concept of cookie sub-domain name

Cookies strictly distinguish domain names.

Supports sharing between subdomain names:

Use the fifth parameter to set

Validity period, valid path, valid Subdomain!

6, $_COOKIE cannot capture the cookie variable set by the current script!

#$_COOKIE is all the cookies carried by the browser when requesting!

The current setting will only be used when requesting next time!

session technology, session technology

Scenario:

Cookie problem

Because the data itself is on the browser side:

Data security issues!

Data must always be carried when requesting!

How to solve it? Pay attention to sharing data between multiple requests of the browser!

Put the data on the server side, and at the same time distinguish the data from the browser, and share the data between multiple requests of the browser!

On the server, add a data space for each browser that visits, and then assign different unique identifiers to these data spaces! Assign a unique identifier to each browser. The identifier of the server-side database space should correspond one-to-one to the

requirement. The browser carries the identifier every time it makes a request. At this time, the server can obtain the identifier and use the identifier. The data space is determined, but all requested data processing is completed within the current determined space!

The unique identifier assigned by the server to the browser is stored in the browser's cookie, which ensures that the browser carries it every time it comes!

The server determines the identification for each new browser access (browser without identification), and generates a unique data space on the server!

Basic use

Directly operate the $_SESSION array to complete the storage and retrieval of session data!

Each session data corresponds to an element in $_SESSION! Operating on elements means operating on session data!

However, session technology, including generating session identifiers, opening up session data space, allocating session identifiers to browsers, etc., all require the support of PHP's session mechanism!

Therefore, you need to enable session support first before you can operate the $_SESSION variable to operate session data!

Open:

session_start();

Operation:

$_SESSION;

Open first and then operate:

Update and delete:

Basic Principle

SessionID saved in browser-side cookie:

When the current browser makes a request to the server for the first time, the server cannot determine the identity of the browser

A unique identifier will be regenerated and saved to the browser in the form of a cookie!

The default cookie variable name is: PHPSESSID.

This cookie scalar is also called sessionID!

When the browser has the sessionid cookie variable, subsequent requests will carry the ID and make requests:

The server side is the session data space

By default, PHP will generate a file in the space where the session data is saved! Distinguish which ID it belongs to by the name of the file!

The default is saved in the temporary directory of the server operating system:

Approximate process:

Test:

Use session to complete login ID verification:

The above is the detailed content of Detailed introduction to cookies and sessions (pictures and text). 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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Where are cookies stored? Where are cookies stored? Dec 20, 2023 pm 03:07 PM

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.

How to solve session failure How to solve session failure Oct 18, 2023 pm 05:19 PM

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.

Where are the cookies on your computer? Where are the cookies on your computer? Dec 22, 2023 pm 03:46 PM

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.

Solution to PHP Session cross-domain problem Solution to PHP Session cross-domain problem Oct 12, 2023 pm 03:00 PM

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

Where are the mobile cookies? Where are the mobile cookies? Dec 22, 2023 pm 03:40 PM

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.

How cookies work How cookies work Sep 20, 2023 pm 05:57 PM

The working principle of cookies involves the server sending cookies, the browser storing cookies, and the browser processing and storing cookies. Detailed introduction: 1. The server sends a cookie, and the server sends an HTTP response header containing the cookie to the browser. This cookie contains some information, such as the user's identity authentication, preferences, or shopping cart contents. After the browser receives this cookie, it will be stored on the user's computer; 2. The browser stores cookies, etc.

Detailed explanation of where browser cookies are stored Detailed explanation of where browser cookies are stored Jan 19, 2024 am 09:15 AM

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.

What are the differences between JavaScript and PHP cookies? What are the differences between JavaScript and PHP cookies? Sep 02, 2023 pm 12:29 PM

JavaScriptCookies Using JavaScript cookies is the most effective way to remember and track preferences, purchases, commissions and other information. Information needed for a better visitor experience or website statistics. PHPCookieCookies are text files that are stored on client computers and retained for tracking purposes. PHP transparently supports HTTP cookies. How do JavaScript cookies work? Your server sends some data to your visitor's browser in the form of a cookie. Browsers can accept cookies. If present, it will be stored on the visitor's hard drive as a plain text record. Now, when a visitor reaches another page on the site

See all articles