Home Backend Development PHP Tutorial Introduction to php and browser caching mechanism_PHP tutorial

Introduction to php and browser caching mechanism_PHP tutorial

Jul 20, 2016 am 11:11 AM
php and introduce exist Will document local mechanism Browse Browser of disk Strategy cache

The browser's caching policy will temporarily cache browsed files on the local disk. When the user repeatedly requests the page, the client is informed that the page has not changed and caching can be called. So how do you know if the client has a page cache? From the HTTP protocol level, when the browser sends a request, it will first send the following

HTTP header:

Connection Keep-Alive
Date Sun, 06 May 2012 18:00:36 GMT
Last-Modified Sun, 06 May 2012 17:31:02 GMT
Etag ec1f629013925ab0fa4389ba926e8c06
Keep-Alive timeout=15, max=299
Server Apache/2.2.16 (Unix) DAV/2
Vary Accept-Encoding

Please note these two lines, which describe the cache information of the page:

Last-Modified Sun, 06 May 2012 17:31:02 GMT
Etag ec1f629013925ab0fa4389ba926e8c06 In this case, if the server responds with a 304 status code, the browser will consciously read the data from the cache; if it responds with a 200 status code, it will still read from the server regardless of whether there is a client cache.

According to this theoretical support, for example, most of the query results of the webmaster army are obtained asynchronously through ajax, and secondary visits can be cached in this way. As long as the client has cache, send a 304 response status code to the client, and then exit the program execution.

The request sent by the browser contains two parameters: If-Modified-Since and If-None-Match:

If-Modified-Since means asking whether the last modification time of the data is a certain time value. Then the server will check the last modification time of the data, and if it is that time, it will return a 304 status code. After receiving the status code, the client will read the cache directly from the local cache. This situation has a precondition, that is, cache resources must exist locally before the browser will send the If-Modified-Since parameter, and the value is the Last-Modified value returned by the last server.

If-None-Match is similar. It is generated by the Etag value returned by the server. It is only used by the server to check the modification time of the data. It can be any value. Considering that the method of If-Modified-Since combined with Last-Modified is not supported by all servers, only the implementation using etag will be considered here.

In PHP, you can use $_SERVER['HTTP_IF_NONE_MATCH'] to determine whether the file is cached by the browser. The code snippet is as follows:

//Use the etag tag to control caching

header('Etag:' . $etag);
The code is as follows
 代码如下 复制代码
$etag = md5(date('Ymd'));
if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
 header('Etag:' . $etag, true, 304);
 exit();
} else {
 header('Etag:' . $etag);
Copy code

$etag = md5 (date('Ymd'));

if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
header('Etag:' . $etag, true, 304);
exit( );

} else {

} Here I use the current date to generate the etag, which ensures The cache will take effect for up to one day, and this parameter can be modified as needed.

Even if it is a 304 response, the server will actually be requested because a connection needs to be established to determine whether data needs to be transmitted. 304 caching saves the overhead of static resource transmission; Another type of cache is the cache in 200 response. No connection is established but the request will respond with 200 status code and the cache is read directly from the local. http://www.bkjia.com/PHPjc/444666.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/444666.htmlTechArticleThe browser’s cache policy will temporarily cache browsed files on the local disk. When the user repeatedly requests the page, the client is informed that the page has not changed and caching can be called. Then...
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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Download the official website of Ouyi Exchange app for Apple mobile phone Download the official website of Ouyi Exchange app for Apple mobile phone Apr 28, 2025 pm 06:57 PM

The Ouyi Exchange app supports downloading of Apple mobile phones, visit the official website, click the "Apple Mobile" option, obtain and install it in the App Store, register or log in to conduct cryptocurrency trading.

Sesame Open Door Official Website Entrance Sesame Open Door Official Latest Entrance 2025 Sesame Open Door Official Website Entrance Sesame Open Door Official Latest Entrance 2025 Apr 28, 2025 pm 07:51 PM

Sesame Open Door is a platform that focuses on cryptocurrency trading. Users can obtain portals through official websites or social media to ensure that the authenticity of SSL certificates and website content is verified during access.

Binance official website entrance Binance official latest entrance 2025 Binance official website entrance Binance official latest entrance 2025 Apr 28, 2025 pm 07:54 PM

Visit Binance official website and check HTTPS and green lock logos to avoid phishing websites, and official applications can also be accessed safely.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

Ouyi official website entrance Ouyi official latest entrance 2025 Ouyi official website entrance Ouyi official latest entrance 2025 Apr 28, 2025 pm 07:48 PM

Choose a reliable trading platform such as OKEx to ensure access to the official entrance.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

How to configure the character set and collation rules of MySQL How to configure the character set and collation rules of MySQL Apr 29, 2025 pm 04:06 PM

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles