Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6814285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:40:16+00:00 2026-05-26T20:40:16+00:00

My cache script is caching my files perfectly the first time they are viewed

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:40:17+00:00Added an answer on May 26, 2026 at 8:40 pm

    Ok I have solved this. The problem was with the spacing in the data property.

    So data: 'cache = yes' needed to become data: 'cache=yes'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use (File System Based) static html files for caching, for some reasons i
I have a Perl script where I maintain a very simple cache using a
I'm needing to cache some data using System.Web.Caching.Cache . Not sure if it matters,
I'm implementing a PHP script which will need some caching capabilities. I'm trying to
I'm working on caching of static resources. Often when a script or style is
I'm trying to implement some kind of caching in a PHP script that will
I have a script that runs CPAN::Mini::Inject actions and has an App::Cache. It works
I'm trying to write a script, which would cache images, but am stuck with
Here is the code: <script type=text/javascript> $.ajaxSetup ({ cache: false }); var ajax_load =
I am working with this script: http://www.cforcoding.com/2009/05/supercharging-javascript-part-4-caching.html Basically the instructions recommend not to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.