if you're site gets many hits per day like wallpaperama you probably have noticed your website has come to its peak. your number of hits is no longer increasing. you've reached the most you can get from you server. and if you are like me, you can't afford to buy those expensive software like zend terminal or a really fast server with multiple CPU and LOTS of memory.

i've noticed that my memory has reached its limit. recently i increased it from 2G to 4G. i have a intel duo core 2.1Hz but that only gets me up to 22,000 hits per day.

so on way to reduce the load on my server from running my dynamic php script, is to have static HTML pages. well, luckly there is a way to make static PHP websites from dynamic content like PHP.

there are many ways, im sure if you are reading my article here its because you found it on a search engine and you've visited some other sites about the subject.

well, what im going to use its called Cache_Lite that comes with PHP.

Cache_Lite allows you to cache your dynamic websites into a file and when that file is retrieved it shows that page.

lets say for example, i have a dynamically generated page:
http://www.example.com/dynamic.php?page=1


so this is the plan:

IF dynamic-1.html IS CACHED {
ECHO dynamic.php?page=1
}ELSE{
SHOW dynamic.php?page=1 AND GENERATE dynamic.php?page=1 TO BE CACHED
}


as you can see, its simple, if the page has been cached, then show the cached page, BUT if it has not, generate the dynamic PHP page, AND save it so next time this page is query bu a user, its shows the cached page

so here we start the tutorial:

step 1. the fist step we are going to do is to create a directory (folder) to put our files from this tutorial. so create a directory called test

step 2. in the test directory, now create another directory called cache - this is where we are going to put our cached files

step 3. Now we make a file called dynamic.php - im using windows xp to write this tutorial, so im going to use notepad to make my php files, you can use your favorite text editor if you dont want to use notepad. so open a new blank notepad and copy and paste the following code:

PHP CODE: dynamic.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dynamic Page By Wallpaperama</title>
</head>
<body>
<h1>Page <? echo $_GET['page']; ?></h1><hr />
<p>Thank you, this is page <? echo $_GET['page']; ?>. <br />
This file is used to generate dynamic content by using the $_GET['page'] to determine what page number to display </p>
<p align="center">Script by <a href="http://www.wallpaperama.com">Wallpaperama.com</a></p>
</body>
</html>


Now upload dynamic.php to the test directory and go to the link. for example:
http://www.example.com/test/dynamic.php?page=1 and you will see its just a simple ordinary php file.

step 4. To make our dynamic.php file more better, lets make our dynamic.php file save the file into cache, so next time you load this page on your browser, it will be get from a file saved in our cache directory. we are going to use PEAR lite_cache to do this. we are also going to use php ob_start() function to start loading our HTML and we use lite_cache to save the html code into a cached file. so now open dynamic.php and chage the code to this:

PHP CODE: dynamic.php
<?
# SCRIPT CREATED BY WALLPAPERAMA.COM * Please do not remove *
$FileName = $_GET['page']; // $FileName HAS TO BE UNIQUE
require_once('Cache/Lite.php'); // INCLUDE LITE.PHP FROM YOUR PEAR PACKAGE
# DECLARE THE OPTIONS
$options = array(
'cacheDir' => 'cache/',
'lifeTime' => 3600
);
# WE PUT OUR OPTION INTO THE Cache_Lite CLASS
$Cache_Lite = new Cache_Lite($options);
if ($HTML = $Cache_Lite->get($_GET['page'])) {
echo '<div style="background-color:#FFCCFF">THIS FILE IS CACHED</div>';
echo $HTML;
}else{
echo '<div style="background-color:#CCFFCC">THIS FILE IS NOT CACHED, BUT I AM CACHING IT</div>';
# START RECORDING OUR CONTENT WITH ob_start()
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dynamic Page By Wallpaperama</title>
</head>
<body>
<h1>Page <? echo $_GET['page']; ?></h1><hr />Pages: <a href="<? echo $_SERVER['PHP_SELF']; ?>?page=1">1</a> <a href="<? echo $_SERVER['PHP_SELF']; ?>?page=2">2</a> <a href="<? echo $_SERVER['PHP_SELF']; ?>?page=3">3</a> <a href="<? echo $_SERVER['PHP_SELF']; ?>?page=4">4</a> <a href="<? echo $_SERVER['PHP_SELF']; ?>?page=5">5</a>
<p>Thank you, this is page <? echo $_GET['page']; ?>. <br />
This file is used to generate dynamic content by using the $_GET['page'] to determine what page number to display </p>
<p align="center">Script by <a href="http://www.wallpaperama.com/forums/how-to-optimize-your-site-and-make-load-faster-with-cache-t6908.html">Back To Wallpaperama Cache Tutorial</a></p>
</body>
</html>
<?
# SAVE THE GENEREATED HTML OUTPUT FROM OUR PHP FILE INTO $HTML STRING
$HTML = ob_get_contents();

# RUN CACHE_LITE TO SAVE THE CONTENTS OF $HTML INTO A FILE IN OUR CACHE/ DIRECTORY
$Cache_Lite->save($HTML,$FileName);
# display the buffer and then flush it
ob_end_flush();
}
?>


after you have copy and paste the code above, upload to your php website and replace it with the first dynamic.php and open it with your browser from your site: example:
http://www.example.com/test/dynamic.php?page=1

NOTE: be sure to give write permissions to the cache directory. so be sure to set the permissions to 777. otherwise, the script wont be able to put the cache files inside the cache/ directory. and this script wont work.

after you click on the 1 2 3 4 5 links, you will see that if the file page has been chaced or not


NOTE: if for some reason you get an error like this:
Warning: main(Cache/Lite.php) [function.main]: failed to open stream: No such file or directory in /myserver/www/dynamic.php on line 4

this means you dont have the PEAR cache system. you can install it by going here:
http://pear.php.net/manual/en/package.caching.cache-lite.php

hope this helps.

if you have any questions or comments, please let me know