在php中设置外部css / js / jpg文件的到期时间

How can we set up the expire time for external css, js and images in php.

I tried below steps but i have issues.

Before the external file is -

<script type="text/javascript" src="https://ssl.google-analytics.com/ga.js"></script>

The next step is to create a .php file. Let’s call it externaljs.php. Insert the following code in it:

<?php
$files = array(
'ga.js' => 'https://ssl.google-analytics.com/ga.js',
'bsa.js' => 'https://s3.buysellads.com/ac/bsa.js',
'pro.js' => 'https://s3.buysellads.com/ac/pro.js');
if(isset($files[$_GET['file']])) {
if ($files[$_GET['file']] == 'ga.js'){
    header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + ((60 * 60) * 48))); // 2 days for GA
} else {
    header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60))); // Default set to 1 hour
}
echo file_get_contents($files[$_GET['file']]);
}
?>

We will send the file name as query param to pass to the php file and can able to set up the expire time.

<script type="text/javascript" src="externaljs.php?file=ga.js"></script>

Now Leverage Browsing Cache displays externaljs.php file has not set expire time

Kindly give us suggestion.

you better do it with htaccess file. the reference link https://gtmetrix.com/leverage-browser-caching.html