Solved

Add Expires Headers - PHP

Posted on 2010-08-24
9
790 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
 
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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 108

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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)

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now