PHP setting cache-control fails

WBOY
Release: 2023-03-01 21:24:02
Original
1703 people have browsed it

This is the request information, you can see that cache-control has been set successfully.
PHP setting cache-control fails

But when requesting, you still have to request it again from the server.

The status here should be 200, but the size should be from cache.

PHP setting cache-control fails

Reply content:

This is the request information, you can see that cache-control has been set successfully.
PHP setting cache-control fails

But when requesting, you still have to request it again from the server.

The status here should be 200, but the size should be from cache.

PHP setting cache-control fails

Refer to the example below

<code><?php 
$cache_time = 3600; 
$modified_time = @$_SERVER['HTTP_IF_MODIFIED_SINCE']; 
if( strtotime($modified_time)+$cache_time > time() ){ 
    header("HTTP/1.1 304"); 
    exit; 
} 
header("Last-Modified: ".gmdate("D, d M Y H:i:s", time() )." GMT");  
echo time(); 
?>
</code>
Copy after login

Open with a browser, we can see that when opened for the first time, the status code returned is 200, and the printing time is the latest time. Then when we open it for the second time, we can see that the status code is 304 and the time is the same as before, indicating that we are using cache. We delete the last_modified.php file, and then open the page for the third time. The browser returns a 404 error. It can be seen that although Last-Modified uses caching, it still needs to initiate an http request to the server every time the page is opened. The browser based on the user's $_SERVER[ 'HTTP_IF_MODIFIED_SINCE'] to determine whether the browser content has expired. If it has not expired, it will return a 304 status and the browser content will be read from the cache.

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!