Solved

Add Expires Headers - PHP

Posted on 2010-08-24
9
833 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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 how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

635 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