


PHP uses HTTP caching protocol principle analysis and application analysis in Apache environment_PHP tutorial
There is also Etag for static pages.
1. Let’s look at the first situation: apache static page
The static page sent by apache to the client generally contains Last-Modified and Etag. These two tags The value comes from the modification time and inode of the static file.
The following is an interception of the header returned by apache to the client
XML/HTML code
Last-Modified: Fri, 26 Jan 2007 01:53:34 GMT
ETag: "3f9f640-318-cb9f8380"
The reason why search engines like static files Because of these two logos, you can determine whether the file has been updated
2. Dynamic pages such as PHP
Since PHP is dynamically generated, its content cannot The last modification date is determined based on the time of the PHP program, so by default PHP does not include any cache control when returning to the client. If you want to make good use of the cache, you must understand the cache mechanism and manage the interaction to reduce b and s, reduce bandwidth traffic, and reduce the load on the server. The burden...the benefits are many.
3. The specific meaning of cache control
First explain the meaning of these tags that I have tested and understood
Cache-Control: Specify The caching mechanism followed by requests and responses. Setting Cache-Control in a request message or response message does not modify the caching process during the processing of another message. The caching instructions during the request include no-cache, no-store, max-age, max-stale, min-fresh, only-if-cached, and the instructions in the response message include public, private, no-cache, no-store, no-transform, must-revalidate, proxy-revalidate, max-age.
The meaning of the instructions in each message is as follows:
Public indicates that the response can be cached by any cache area.
Private indicates that all or part of the response message for a single user cannot be processed by the shared cache. This allows the server to only describe a partial response from a user that is not valid for other users' requests.
no-cache indicates that the request or response message cannot be cached.
no-store is used to prevent important information from being released unintentionally. Sending it in the request message will cause both the request and response messages to use caching.
max-age indicates that the client can receive responses with a lifetime no longer than the specified time in seconds.
min-fresh indicates that the client can receive responses with a response time less than the current time plus the specified time.
max-stale indicates that the client can receive response messages beyond the timeout period. If you specify a value for max-stale messages, the client can receive response messages that exceed the specified value of the timeout period.
php usage:
Use header() before output, (if you use ob_start(), you can place the header anywhere in the program)
PHP code
header('Cache-Control: max-age=8');
max-age=8 means the maximum lifetime is 8 seconds. If it exceeds 8 seconds, the browser must go to the server to read it again. This time starts from the time the user reads the page, and Expires is an absolute time.
Expires: The absolute time for cache expiration. If it passes the specified time point, the browser will not recognize the cache and will go to the server to request the latest one again.
Last-Modified: The last modification time of the document. Its wonderful use is: 1
If it is a static file, the client will send the time in its cache, and apache will compare it. If it is found that there is no modification, a header will be returned directly. The status code is 304 and the number of bytes is very small. (The advanced version will also add a comparison Etag to determine whether the file has changed)
2 PHP dynamic file:
The client sends the comparison time, and PHP will determine whether to modify it. If the modification time is the same, only 1024 bytes will be returned. As for why 1024 is returned, it is unknown. If the file generated by your PHP is very large, it will only Returns 1024, so it saves bandwidth. The client will automatically display it from the cache file based on the modification time sent by the server.
Note: Cache-Control and Expires can also work without the Last-Modified header, but each request must return the real number of file bytes, not 1024
4. HOW?
You don’t need to worry about static pages. If you want to better control the caching of static pages, apache has several modules that can be well controlled, which will not be discussed here
php page:
There are two types here:
1. Pages that do not change frequently, similar to news releases. The characteristics of this type of page: there will be several times after the first release. Changes will basically not be modified over time. The control strategy should be: 1. Send Last-Modified for the first release, set max-age for 1 day, update Last-Modified after modification, and max-age time will be normal with the number of modifications. This seems more cumbersome, and you have to record the number of modifications. You can also predict the next possible modification time and use Expires to specify the approximate time to expire
PHP code
//header('Cache-Control: max-age=86400');//Cache for one day
header('Expires: Mon, 29 Jan 2007 08:56:01 GMT');/ /Specify expiration time
header('Last-Modified: '.gmdate('D, d M Y 01:01:01',$time).'GMT');//Greenwich time, $time is the time when the file is added Timestamp
2 Frequently changed pages
are similar to bbs and forum programs. This kind of page updates faster. The main function of the cache is to prevent users from frequently refreshing the list, causing the server to The burden on the database must not only ensure the timeliness of updates, but also ensure that the cache can be utilized
This is generally controlled by Cache-Control, and max-age can be flexibly controlled according to the frequency of postings in the forum.
PHP code
header('Cache-Control: max-age =60');//Cache for one minute
header('Last-Modified: '.gmdate('D, d M Y 01:01:01',$time).'GMT');//Greenwich time, $time is the last updated timestamp of the post
Five extra
1 The difference between refresh, go to, force refresh
Browser There are refresh and go buttons on the browser. Some browsers support using ctrl+F5 to force refresh the page. What is the difference between them?
Go to: When the user clicks on the link, it will go to it. It completely uses the caching mechanism. If there is Last-Modified, it will not communicate with the server. You can use the packet capture tool to see that the sent byte is 0byte. If the cache expires , then it will perform the F5 refresh action.
Refresh (F5): This refresh is also determined based on whether the cache has Last-Modified. If so, it will be transferred to 304 or 1024 (php). If there is no last update time, then go to the server to read it and return the true value. Document size
Forced refresh: completely abandon the cache mechanism, go to the server to read the latest document, and send the header to the server as follows
XML/HTML code
Cache-Control: no-cache
2 Debugging Tools
View browser and A better tool for server interaction is httpwatch pro, the current version 4.1, supports ie7
There are other proxy packet capture tools that can analyze, http debugging. I haven’t used it before, but there is also a tcp packet capture tool, the network that comes with 2000. There are also tcp packet capture tools. The network monitor that comes with 2000 is not specifically for http, but it is difficult to use.

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

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

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











PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

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 is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip
