Skip to content

Instantly share code, notes, and snippets.

@walterdavis
Created February 19, 2016 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walterdavis/f8189c60da6abc8660f8 to your computer and use it in GitHub Desktop.
Save walterdavis/f8189c60da6abc8660f8 to your computer and use it in GitHub Desktop.
<?php
// Autor: gallir@uib.es, header implementation for weak y strong
// HTTP validator according to RFC2616 of HTTP/1.1
// It's also compatible with HTTP/1.0 and
// take care of Netscape non-standard headers
// If more than 10800 secs have gone since last load
// it makes the client/cache to reload the page
// Call this function before doing anything else with databases or similar.
if(function_exists('getallheaders')){
function DoHeaders($lastModified, $logged=FALSE) {
// $lastModified: last database modification, in "epochs"
// $logged: FALSE/TRUE // It takes in account that pages can be accessed
// as public or authorised
$now = time();
$maxCache = $now - 10800; // Max. time before refreshing
$headers = getallheaders();
$refresh=TRUE; // refresh, as default
if(isset($headers["If-Modified-Since"])) {
// Netscape sends ";length = xxx" after the date
$arraySince = explode(";", $headers["If-Modified-Since"]);
$since = strtotime($arraySince[0]);
if($since >= $lastModified) $refresh=FALSE;
}
if($logged == TRUE) {
$tag="\"AUT".$lastModified."\""; // A private page
} else {
$tag="\"PUB".$lastModified."\""; // and public one
}
if(isset($headers["If-None-Match"])) { // check ETag
if(strcmp($headers["If-None-Match"], $tag) == 0 ){
$refresh=FALSE;
}else{
$refresh=TRUE;
}
}
if(!$refresh) {
header("HTTP/1.1 304 Not changed");
// The first header must be this
// otherwise Netscape gives "No Data" error
$strLastModified = gmdate("r", $lastModified);
}else{
$strLastModified = gmdate("r", $now);
}
header("Content-type: text/html; charset=utf-8");
header("ETag: $tag"); // The new TAG
header("Last-Modified: $strLastModified");
header("Expires: " . gmdate("r", time()+1));
header("Cache-Control: max-age=1, must-revalidate"); // HTTP/1.1
// Netscape doesn't handle very well the header("Pragma: no-cache");
if(!$refresh) {
ob_end_clean(); // Just in case..
die; // Don't do anything more
}
ob_start(); // We start buffering to allow "Content-length" header
// at the end of the output to allow HTTP/1.0 persistent connections.
}
}else{
function DoHeaders($lastModified, $logged=FALSE) {
// $lastModified: last database modification, in "epochs"
// $logged: FALSE/TRUE // It takes in account that pages can be accessed
// as public or authorised
$now = time();
$maxCache = $now - 10800; // Max. time before refreshing
//$headers = getallheaders();
$refresh=TRUE; // refresh, as default
if(isset($_SERVER["If-Modified-Since"])) {
// Netscape sends ";length = xxx" after the date
$arraySince = explode(";", $_SERVER["If-Modified-Since"]);
$since = strtotime($arraySince[0]);
if($since >= $lastModified) $refresh=FALSE;
}
if($logged == TRUE) {
$tag="\"AUT".$lastModified."\""; // A private page
} else {
$tag="\"PUB".$lastModified."\""; // and public one
}
if(isset($_SERVER["If-None-Match"])) { // check ETag
if(strcmp($_SERVER["If-None-Match"], $tag) == 0 ){
$refresh=FALSE;
}else{
$refresh=TRUE;
}
}
if(!$refresh) {
header("HTTP/1.1 304 Not changed");
// The first header must be this
// otherwise Netscape gives "No Data" error
$strLastModified = gmdate("r", $lastModified);
}else{
$strLastModified = gmdate("r", $now);
}
header("Content-type: text/html; charset=utf-8");
header("ETag: $tag"); // The new TAG
header("Last-Modified: $strLastModified");
header("Expires: " . gmdate("r", time()+1));
header("Cache-Control: max-age=1, must-revalidate"); // HTTP/1.1
// Netscape doesn't handle very well the header("Pragma: no-cache");
if(!$refresh) {
ob_end_clean(); // Just in case..
die; // Don't do anything more
}
ob_start(); // We start buffering to allow "Content-length" header
// at the end of the output to allow HTTP/1.0 persistent connections.
}
}
// Add following code at the end of your output
// If we allowed buffering before, we send the length
// to allow HTTP/1.0 persistent connections
function SendLength() {
if(ob_get_length()) {
header("Content-Length: " . ob_get_length());
ob_end_flush();
die; // Delete it if you don't want to finish the script
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment