Creating a cache on phpWebSite

Code Library

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;
}}

Last Reviewed

This article was last reviewed on May 2, 2026 for accuracy and relevance.

Ready for the Next Step?

If this connects to a real challenge your team is facing, I can help you map a next step.

Book a discovery call

Rate And Review This Content

Found this useful? Leave a quick rating and short review. Approved submissions are stored as testimonials.