Table of Contents
php static files return
Home Backend Development PHP Tutorial PHP static file return_PHP tutorial

PHP static file return_PHP tutorial

Jul 13, 2016 am 09:55 AM
return

php static files return

Sometimes some static files (such as pictures) will be output by php, and you will find that the request is 200, and the static files go to the server every time The request is too wasteful of resources. How to let the browser cache the image? We need to output 304 in php.

We can use HTTP_IF_MODIFIED_SINCE in php combined with etag to do this. Etag does not have a clearly defined format. We can use the md5 value of the file modification time. The code is as follows:

The code is as follows:

private function _addEtag($file) {

 $last_modified_time = filemtime($file);

 $etag = md5_file($file);

// always send headers

Header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");

header("Etag: $etag");

// exit if not modified

 if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time ||

@trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {

Header("HTTP/1.1 304 Not Modified");

exit;

 }

 }

It can be called in the code before static files (such as pictures) are output.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/991645.htmlTechArticlephp static file return Sometimes some static files (such as pictures) will be output by php, and you will find that the requests are all 200, It is a waste of resources to request static files from the server every time. How to make the browser...
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
4 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)

PHP Tips: Quickly Implement Return to Previous Page Function PHP Tips: Quickly Implement Return to Previous Page Function Mar 09, 2024 am 08:21 AM

PHP Tips: Quickly implement the function of returning to the previous page. In web development, we often encounter the need to implement the function of returning to the previous page. Such operations can improve the user experience and make it easier for users to navigate between web pages. In PHP, we can achieve this function through some simple code. This article will introduce how to quickly implement the function of returning to the previous page and provide specific PHP code examples. In PHP, we can use $_SERVER['HTTP_REFERER'] to get the URL of the previous page

What results does MySQL return after inserting data? What results does MySQL return after inserting data? Mar 01, 2024 am 10:27 AM

MySQL is a widely used relational database management system for storing and managing data. When we want to insert new data into a database table, we usually use the INSERT statement. In MySQL, when the INSERT statement is executed to successfully insert data, a result will be returned, which is the result of the insertion operation. In this article, we will discuss in detail the results returned by MySQL after inserting data and provide some specific code examples. 1. The result returned after inserting data is in MySQL. When successfully executed

How to use Vue to implement the return to previous page effect How to use Vue to implement the return to previous page effect Sep 19, 2023 pm 01:07 PM

How to use Vue to implement the special effect of returning to the previous page. In front-end development, we often encounter situations where we need to return to the previous page. By adding a back button, you can provide a better user experience. This article will introduce how to use the Vue framework to achieve the special effect of returning to the previous page, and provide corresponding code examples. First, in the Vue project, you need to create a page as the previous page. We can set routing through VueRouter, and each route corresponds to a component. In the previous page, we can add a back button and pass the click event

In an extended matrix, return the previous element in C++ In an extended matrix, return the previous element in C++ Sep 15, 2023 am 09:17 AM

Discuss a problem based on the extended matrix. An extended matrix is ​​a matrix whose size increases by some factor. Here we have a character matrix whose size is expanded by a multiple of 2, i.e. if the size of the original matrix is ​​N*N, then the size of the expanded matrix becomes 2N*2N. We are given a character sequence located at (i, j), and we need to return the character sequence located at (i, (j-N-1)%N). Let's understand by visualizing some initial expansion matrices. GivenMatrix->[a,b][c,d],2X2matrixMultiplyingwith{a,b,c,d}AX[a,b]BX[a,b]CX[a,b]DX[a,b][c ,d]

Java program returns the largest element in a list Java program returns the largest element in a list Aug 19, 2023 pm 05:17 PM

We can use array loop to return the largest element from the list. This is mainly achieved by comparing models. In a list, the largest number is compared to all elements in the list. The procedure will consider 'n' as input quantity and store it as data value in the array. Afterwards, the program will display the largest element on the output console after the loop ends. In this article, we will help you understand and write some Java code through which you can find the largest element from an array list. How to select the largest number from an array using Java? Wecanfindalargestnumberbysortinganarray.TodefineavoidArrayL

Write a function in C programming that returns 2 when the input is 1 and 1 when the input is 2 Write a function in C programming that returns 2 when the input is 1 and 1 when the input is 2 Sep 10, 2023 pm 01:25 PM

You need to make a function that returns 2 for input 1 and 1 for input 2. This function can be made in various ways depending on the logic you use. The simplest way is to use a conditional statement, if the number is 1, return 2, otherwise return 1, other ways include using math operations (any kind will work) and XOR operations. Example #include<stdio.h>//Method1usingtheifstatementintreverseif(intx){ if(x==1)return2; elsereturn1;}/

How to return the value of a PHP custom function? How to return the value of a PHP custom function? Apr 15, 2024 pm 05:00 PM

Custom functions in PHP can return values ​​of specified types through the return statement, including strings, numbers, arrays, and objects. Practical case: -Return string: functiongreet($name){return "Hello,$name!";} -Return array: functionget_user_data($id){return["name"=>"John","email"=> "john@example.com"];}

How to create a function with a return value using PHP? How to create a function with a return value using PHP? Apr 10, 2024 pm 12:45 PM

The steps for using function return values ​​in PHP include: using function to declare a function; using the return statement to return results; calling the function and capturing the return value.

See all articles