Creating a cache on phpWebSite

Christopher Ross

2 min read

WordPress & CMS engineering · Fort Erie, Ontario

Editorial photograph of a warm university-style library archive aisle in low tungsten light: tall wood shelves of cared-for leather-bound volumes, an open book on a small ledge under a brass desk lamp with a green glass shade, dust motes catching the warm light, a soft window glow at the far end of the aisle: old but still worth keeping.

I’ve been going nuts for the past month or so, looking for a static cache for phpWebsite, a great CMS application.

In the end, I’ve hacked together a little something and it works perfectly, all you have to do is paste this code in your index.php file, and create a file called “staticindex.html” in your root folder (chmod 777 of course).

Now, the site will check the age of staticindex.html every 5 minutes, if it’s too old it will generate a new one, otherwise it’ll serve up the static version.

if ($_GET[’cache’] == ‘1′) {

} else {
if (empty($_GET) && empty($_POST) && empty($_SESSION[”OBJ_user”])) {

$fileage = date(’U’)-filemtime(’staticindex.html’);
// if it’s new, show it
if ($fileage < 300) {

echo file_get_contents(‘staticindex.html’);

} else {

// Open the file and erase the contents if any
$fp = fopen(“staticindex.html”, “w”);

// Write the data to the file
fwrite($fp, implode(chr(13),file(‘http://www.yourwebsite.com/index.php?cache=1’)));

// Close the file
fclose($fp);
echo file_get_contents(‘staticindex.html’);

}

die;
}}

Keep reading

Working through something on your own site? Get in touch →

Leave a Reply

Your email address will not be published. Required fields are marked *

Your rating (optional)

Your name and email are stored with your comment; only your display name is shown publicly. See our privacy policy.