My cache script is caching my files perfectly the first time they are viewed however when I try to force it to recache a file nothing happens.
First thing’s first this code is located on a file called header.php included at the top of every page:
<?php
$requestURI = rtrim($_SERVER['REQUEST_URI'], '/');
if (stripos($requestURI,'/admin') != false) { //Don't cache admin pages
$cache_time = 0;
} else{
$cache_time = 31556926;
}
$cache_folder = $_SERVER['DOCUMENT_ROOT'].'/cache/';
$cache_filename = $cache_folder.md5($requestURI).'.txt'; //Create unique name for cache
$cache_created = (file_exists($cache_filename)) ? filemtime($cache_filename) : 0;
if ($_POST['cache'] != "yes") { // Allow function to be overridden
if ((time() - $cache_created) < $cache_time) {
readfile($cache_filename); // The cached copy is still valid use it.
die();
}
}
ob_start( );
?>
It checks whether a cached file already exists for this page and whether it has expired or not.
This code is located on a file called footer.php included at the bottom of every page:
<?php
$html = ob_get_clean();
$config = array('indent' => TRUE,
'drop-empty-paras' => FALSE,
'output-xhtml' => TRUE,
'quote-ampersand' => TRUE,
'indent-cdata' => TRUE,
'tidy-mark' => FALSE,
'wrap' => 200);
$tidy = tidy_parse_string($html, $config, 'UTF8'); //Clean HTML output
file_put_contents($cache_filename, $tidy->value); //Create or update cache
mysql_select_db('reithg_UNITS');
$url = $_SERVER['DOCUMENT_ROOT'].$requestURI;
$cache = md5($requestURI);
$query = mysql_query("SELECT * FROM Cache WHERE CACHE = '$cache'") or die(mysql_error());
$exists = mysql_num_rows($query);
function getIP() //Get the client's IP
{
if (isset($_SERVER["HTTP_CLIENT_IP"]))
{
return $_SERVER["HTTP_CLIENT_IP"];
}
elseif (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
return $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif (isset($_SERVER["HTTP_X_FORWARDED"]))
{
return $_SERVER["HTTP_X_FORWARDED"];
}
elseif (isset($_SERVER["HTTP_FORWARDED_FOR"]))
{
return $_SERVER["HTTP_FORWARDED_FOR"];
}
elseif (isset($_SERVER["HTTP_FORWARDED"]))
{
return $_SERVER["HTTP_FORWARDED"];
}
else
{
return $_SERVER["REMOTE_ADDR"];
}
}
$IP = getIP();
if ($exists != 0) {
$cached = mysql_fetch_row($query);
$cachedID = $cached[0];
mysql_query("UPDATE Cache SET URL = '$url', CACHE = '$cache', UPDATED = NOW(), `UPDATED BY` = '$IP', WHERE ID = $cachedID;") or die(mysql_error());
} else {
mysql_query("INSERT INTO Cache (ID, URL, CACHE, CREATED, UPDATED, `CREATED BY`, `UPDATED BY`) VALUES('NULL', '$url', '$cache', NOW(), 'NULL', '$IP', 'NULL') ") or die(mysql_error());
}
echo $tidy;
?>
It creates the cache file, and gathers some information about it’s generation for the database.
Then I have an ajax script located at another location which generates a list of all my cached pages and sends cache = yes via POST to each one which should force it recache. (Stops it reading the cached file), but nothing happens and the cached file remains the same. Here is the jquery script after it is generated by PHP:
function recache(){
var request1 = $.ajax({type: 'POST',url: '../units_and_evidence/flash_delivery.php?id=1',data: 'cache = yes'});
var request2 = $.ajax({type: 'POST',url: '../',data: 'cache = yes'});
var request3 = $.ajax({type: 'POST',url: '../units_and_evidence',data: 'cache = yes'});
var request4 = $.ajax({type: 'POST',url: '../units_and_evidence/flash_delivery.php?id=15',data: 'cache = yes'});
function successful () {
alert("Reset cache success");
}
function failure () {
alert("Reset cache failed");
}
var recache = $.when(request1,request2,request3,request4).then(successful, failure);
}
It returns the successful() function so the POST is successful, thus it must be an issue with header.php or footer.php
Ok I have solved this. The problem was with the spacing in the
dataproperty.So
data: 'cache = yes'needed to becomedata: 'cache=yes'.