Solved

Add Expires Headers - PHP

Posted on 2010-08-24
9
801 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
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 110

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

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

738 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