There are many tools around that will help cache your web pages to increase speed and performance (most notably Memcached) however these usually require installation on your web server, thus rendering them useless for people who use shared hosting and don't have access to the server.

Using some simple PHP code we can quite effectively cache a webpage, useful for sites that are database driven and have a large amount of queries or high volumes of traffic.

First we need to setup the folder where the cached files are stored, making sure it has read/write permissions (CHMOD to 0777 - see your FTP client documentation on how to do this).

Next create a file called cache.php which contains the following code:

<?php

class Simple_Cache {

	// Number of seconds a page should remain cached for
	public $cache_expires = 3600;

	// Path to the cache folder
	public $cache_folder = "/home/usr/www/cache/";

	// Include query strings to make the cached page file unique
	public $include_query_strings = true;

	// The current cache file, this will get set when loaded
	private $cache_file = "";

	/**
	 * Set the current cache file from the page URL
	 */
	public function __construct() {
		$file = $_SERVER['REQUEST_URI'];
		if (!$this->include_query_strings && strpos($file, "?") !== false) {
			$qs = explode("?", $file);
			$file = $qs[0];
		}

		$this->cache_file = $this->cache_folder . md5($file) . ".html";
	}

	/**
	 * Checks whether the page has been cached or not
	 * @return boolean
	 */
	public function is_cached() {
		$modified = (file_exists($this->cache_file)) ? filemtime($this->cache_file) : 0;
		return ((time() - $this->cache_expires) < $modified);
	}

	/**
	 * Reads from the cache file
	 * @return string the file contents
	 */
	public function read_cache() {
		return file_get_contents($this->cache_file);
	}

	/**
	 * Writes to the cache file
	 * @param string $contents the contents
	 * @return boolean
	 */
	public function write_cache($contents) {
		return file_put_contents($this->cache_file, $contents);
	}
}

// Initiate the cache class
$cache = new Simple_Cache();

// Check if the page has already been cached and not expired
// If true then we output the cached file contents and finish
if ($cache->is_cached()) {
	echo $cache->read_cache();
	exit();
}

// Ok so the page needs to be cached
// Turn on output buffering
ob_start();

Now in your page you would include cache.php on the first line before you do anything else. This is because if the page is cached we don't want it to do anything else other than output the cache to the page. If you are still including other files or connecting to databases before checking for the cache you are putting additional load on the server, making this exercise pointless.

Finally create a file called cache_footer.php which contains the following code:

<?php
// Grab the uncached page contents
$cache_contents = ob_get_contents();
ob_end_flush();

// Save it to the cache for next time
$cache->write_cache($cache_contents);
?>

You would include this file on the last line of your page, which saves the page in the cache if required.

Putting it altogether a typical page would look like this:

<?php
// Load the cache process
include("cache.php");

// Connect to database
include("config.php");
mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
?>
<html>
<body>
<h1>Articles</h1>
<ul>
<?php
// Some query
$query = mysql_query("SELECT * FROM articles ORDER BY id");
while ($row = mysql_fetch_array($query)) {
  echo '<li><a href="view_article.php?id='.$row['id'].'">'.$row['title'].'</a></li>';
}
?>
</ul>
</body>
</html>
<?php
// Save the cache
include("cache_footer.php");
?>