Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache control and expires header for PHP

I'm setting my headers

$offset = 60 * 15;

header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
header("Cache-Control: max-age=$offset, must-revalidate"); 

However when running FireBug its giving me the following header information

HTTP/1.1 200 OK
Date: Mon, 25 Jul 2011 12:15:12 GMT
Server: Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i PHP/5.2.9
X-Powered-By: PHP/5.2.9
Expires: Sat, 01 Jan 2000 00:00:01 GMT
Cache-Control: post-check=0, pre-check=0, max-age=0
Pragma: no-cache
Last-Modified: Mon, 25 Jul 2011 12:15:13 GMT
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html

Anyone know why my headers aren't being recognised?

like image 904
Brob Avatar asked Jul 25 '11 12:07

Brob


People also ask

What is the cache-control HTTP headers?

The Cache-Control HTTP Headers is part of the HTTP 1.1 standard. Here you are an example: max-age=seconds – the number of seconds from the time of the request you wish this object to be keep into the cache;

How to use the expire header in PHP?

Using the Expire header is really simple. It tells when the page the browser or the proxy downloaded should be fetched again from the web server. In order to use it in your CGI or PHP page, just after theContent-type, you can add the the expire header as shown below:

What HTTP headers should I set in PHP?

If you are just taking modern browsers into consideration, you need to set only "Cache-Control" and "ETag" headers. "Expires" and "Last-Modified" headers belong to an older HTTP specification and you can leave them out. This tutorial will discuss how you can set "Cache-Control" and "ETag" headers through PHP.

How to set the max age of the Cache-Control header?

Setting the "Cache-Control" header is direct. Just use the required directives for "Cache-Control" and send the header through the header function. header ('Cache-Control: max-age=86400'); Please note that you must use this function before any output from the script is emitted.


2 Answers

I know that few versions ago, Firebug had problems and all requests were 200 instead 304. Here is my code, that i'm using for css:

    <?php
    if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
      $if_modified_since = preg_replace('/;.*$/', '',   $_SERVER['HTTP_IF_MODIFIED_SINCE']);
    } else {
      $if_modified_since = '';
    }

    $mtime = filemtime($_SERVER['SCRIPT_FILENAME']);
    $gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';

    if ($if_modified_since == $gmdate_mod) {
      header("HTTP/1.0 304 Not Modified");
      exit;
    }

    header("Last-Modified: $gmdate_mod");
    header('Content-type: text/css');

    header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (60*60*24*45)) . ' GMT');
    // rest of the code
    ?>
like image 102
JercSi Avatar answered Oct 01 '22 09:10

JercSi


try this

<META HTTP-EQUIV="Pragma" CONTENT="private">
<META HTTP-EQUIV="Cache-Control" CONTENT="private, max-age=5400, pre-check=5400">
<META HTTP-EQUIV="Expires" CONTENT="<?php echo date(DATE_RFC822,strtotime("1 day")); ?>">

or set the headers in htaccess file. You also need to check your apachi config file for cache configration

like image 43
Developer Avatar answered Oct 01 '22 07:10

Developer