Solved

Add Expires Headers - PHP

Posted on 2010-08-24
9
796 Views
Last Modified: 2012-05-10
Hi Experts,

Can you please tell me how to efficiently add expires headers to html elements please? gtmetrix.com is saying that expires headers are normally used with images but should also be used with all page componants including scripts, css and flash.

How can this be done? I'm using PHP if that helps.

Thank you
0
Comment
Question by:wilson1000
9 Comments
 
LVL 3

Expert Comment

by:WabbitSeason
ID: 33508861
You would normally add such headers by either configuring the webserver (for example using a .htaccess) or by serving the page components through PHP. The first method is preferred, as it doesn't require PHP to be loaded for every component. Have a look at http://www.askapache.com/htaccess/apache-speed-expires.html for an example using .htaccess.
0
 
LVL 14

Accepted Solution

by:
svgmuc earned 450 total points
ID: 33508875
An Expires header is added to a document downloaded from a web server. This could be a html file, an image or any other kind of document file.

Expires headers can be configured in your web server by adding this line to your apache configuration or a .htaccess file:

ExpiresActive On
ExpiresByType image/gif "modification plus 5 hours 3 minutes"
ExpiresByType text/css "now plus 5 minutes"

Please see http://httpd.apache.org/docs/2.2/mod/mod_expires.html for documentation.

Also, since you mentioned PHP, you can send headers via PHP by adding this line to your code:

header("Expires: Tue, 24 Aug 2010 05:00:00 GMT");
0
 
LVL 4

Author Comment

by:wilson1000
ID: 33508998
Hi, thanks gentleman.

I have tried the PHP header() function but this is returning no results within the live web test so I'll pass this over to our backend developer for changes to server config.

I'll return soon!

Thanks again




0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 4

Author Comment

by:wilson1000
ID: 33509244
Hi, quick question...


@ svgmuc: How do we format the code for server config? I take it we remove the quotes ""?
0
 
LVL 4

Author Comment

by:wilson1000
ID: 33509298
Hey don't worry... I followed your link and found the resource at apache.
0
 
LVL 5

Expert Comment

by:onemadeye
ID: 33509439
I use this codes on my development :


<?php

header("Expires: Thu, 17 May 2001 10:17:17 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Pragma: no-cache");

ob_start();

?>

Open in new window

0
 
LVL 4

Author Comment

by:wilson1000
ID: 33509660
I see what you're saying however, I need to instruct the user agent to cache a vast majority of files that we distribute with our page.

Your expires header looks to be initializing no cache at all.

As I said before, I've used the code below to no avail and would like the server to take care of this... running test now so will get back in a short time...

$expires = 60*60*24*14;
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');

Open in new window

0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 total points
ID: 33511213
For the most part, the server should do all of this for you.  If you force a download, you probably do not want it to be cached.  You can use something like this to handle that "edge case."
<?php // RAY_force_download.php
error_reporting(E_ALL);


// A FILE TO DOWNLOAD - THIS LINK COULD COME IN THE URL VIA $_GET OR COULD BE GENERATED INSIDE THE SCRIPT
$url = "http://www.google.com/intl/en_ALL/images/logo.gif";

// USE CASE
force_download($url);


// FUNCTION TO FORCE A DOWNLOAD
function force_download($filename)
{
    // GET A NAME FOR THE FILE
    $basename = basename($filename);

    // GET THE CONTENTS OF THE FILE
    $filedata = file_get_contents($filename);

    if ($filedata)
    {
        // THESE HEADERS ARE USED ON ALL BROWSERS
        header("Content-Type: application-x/force-download");
        header("Content-Disposition: attachment; filename=\"$basename\"");
        header("Content-length: ".(string)(strlen($filedata)));
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

        // THIS HEADER MUST BE OMITTED FOR IE 6+
        if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE '))
        {
            header("Cache-Control: no-cache, must-revalidate");
        }

        // THIS IS THE LAST HEADER
        header("Pragma: no-cache");

        // FLUSH THE HEADERS TO THE BROWSER
        flush();

        // CAPTURE THE FILE IN THE OUTPUT BUFFERS - WILL BE FLUSHED AT SCRIPT END
        ob_start();
        echo $filedata;
    }
}

Open in new window

0
 
LVL 4

Author Closing Comment

by:wilson1000
ID: 33519340
Thank you,

I was only going to award points to @svgmuc for his direction as this is what I needed however, Ray offered a great solution to cover required downloads.

0

Featured Post

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question