How to Use Http Cache Headers With PHP

How to use HTTP cache headers with PHP

You might want to use private_no_expire instead of private, but set a long expiration for content you know is not going to change and make sure you process if-modified-since and if-none-match requests similar to Emil's post.

$tsstring = gmdate('D, d M Y H:i:s ', $timestamp) . 'GMT';
$etag = $language . $timestamp;

$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false;
$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] : false;
if ((($if_none_match && $if_none_match == $etag) || (!$if_none_match)) &&
($if_modified_since && $if_modified_since == $tsstring))
{
header('HTTP/1.1 304 Not Modified');
exit();
}
else
{
header("Last-Modified: $tsstring");
header("ETag: \"{$etag}\"");
}

Where $etag could be a checksum based on the content or the user ID, language, and timestamp, e.g.

$etag = md5($language . $timestamp);

How to set HTTP headers (for cache-control)?

To use cache-control in HTML, you use the meta tag, e.g.

<meta http-equiv="Cache-control" content="public">

The value in the content field is defined as one of the four values below.

Some information on the Cache-Control header is as follows

HTTP 1.1. Allowed values = PUBLIC | PRIVATE | NO-CACHE | NO-STORE.

Public - may be cached in public shared caches.

Private - may only be cached in private cache.

No-Cache - may not be cached.

No-Store - may be cached but not archived.

The directive CACHE-CONTROL:NO-CACHE indicates cached information should not be used
and instead requests should be forwarded to the origin server. This directive has the same semantics as the PRAGMA:NO-CACHE.

Clients SHOULD include both PRAGMA: NO-CACHE and CACHE-CONTROL: NO-CACHE when a no-cache request is sent to a server not known to be HTTP/1.1 compliant. Also see EXPIRES.

Note: It may be better to specify cache commands in HTTP than in META statements, where they can influence more than the browser, but proxies and other intermediaries that may cache information.

How to prevent Browser cache for php site

try this

<?php

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>

how to use control-cache headers?

The key is must-revalidate: This means, that the client is asking the server if the file has changed. If you don’t handle this case, the browser will fetch a new copy.

Read Mark Nottingham’s fantastic Caching Tutorial for more information.
As an example for a PHP implementation you may use my code.

Look into $_SERVER['HTTP_IF_NONE_MATCH']and $_SERVER['HTTP_IF_MODIFIED_SINCE'] for validating clients. And be aware that both headers may contain malicious code. ;)

What HTTP headers for static resources to be correctly cached by CloudFlare?

Cloudflare respects what you put in HTTP headers. You just need to set a cache-Control: max-age value when returning your images. See this SO question.

using HTTP headers for cache control in PHP

first of all you will have to parse the date given in the headers:

$header = "Expires: Mon, 22 Nov 2011 10:01:22 GMT";
preg_match(
"/Expires: [A-Za-z]+, ([0-9]{1,2}) ([A-Za-z]+) ([0-9]{4}) ([0-9:]+) ([A-Z]{3})/",
$header,
$matches
);
$months = array(
"Jan" => "01",
"Feb" => "02",
"Mar" => "03",
"Apr" => "04",
"May" => "05",
"Jun" => "06",
"Jul" => "07",
"Aug" => "08",
"Sep" => "09",
"Oct" => "10",
"Nov" => "11",
"Dec" => "12"
);
$day = $matches[1];
$month = $months[$matches[2]];
$year = $matches[3];
$time = $matches[4];
$zone = $matches[5];

$date = new DateTime("$year-$month-$day $time", new DateTimeZone($zone));

then you can check this against the actual time and only execute the update if the $date from the last update is reached

$now = new DateTime();
if($now > $date);

you should save the $date in a file or DB after you downloaded the XML and parsed the date.
next time you execute the script just check the saved date against the new DateTime() to see if you already have to update it, if not you can load the saved XML from the file.

saving the XML as file is good, you might save it to a Database, but I would not set up a DB only for saving one XML-Structure. If you want to keep a history of old XMLs the DB makes sense again.

PHP cache header override

Well, apparently this has no answer. So, my solution at this point is to eliminate the .htaccess code altogether, and apply explicit headers to each file. A pain in the you-know-what but it's time to move on. If anyone has a more elegant solution that can work with an .htaccess default please feel free to share... thanks

Cache Headers on PHP file not working

Source: PHP: Worry about some magical added “Cache-Control” Header ?

These headers are automatically set by the PHP Session module to
prevent browser/proxy based caching of your pages. Depending on your
environment setup, it’s possible to control these headers by using the
session_cache_limiter() function or use the php.ini

To disable these behaviour just pass an empty string to the
session_cache_limiter() function as mentioned in the documentation:

session_cache_limiter('');


Related Topics



Leave a reply



Submit