Link to home
Start Free TrialLog in
Avatar of wilson1000
wilson1000

asked on

Add Expires Headers - PHP

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
Avatar of WabbitSeason
WabbitSeason

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.
ASKER CERTIFIED SOLUTION
Avatar of svgmuc
svgmuc
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wilson1000

ASKER

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




Hi, quick question...


@ svgmuc: How do we format the code for server config? I take it we remove the quotes ""?
Hey don't worry... I followed your link and found the resource at apache.
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

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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.