4

I would like to create a cache for my php pages on my site. I did find too many solutions but what I want is a script which can generate an HTML page from my database ex:

I have a page for categories which grabs all the categories from the DB, so the script should be able to generate an HTML page of the sort: my-categories.html. then if I choose a category I should get a my-x-category.html page and so on and so forth for other categories and sub categories.

I can see that some web sites have got URLs like: wwww.the-web-site.com/the-page-ex.html

even though they are dynamic.

thanks a lot for help

1
  • I think that your question needs some clarification: Are you looking to create a cache in memory of typically used resources from the database, or are you looking for how to dynamically create pages from information in a database? Commented Dec 10, 2008 at 12:14

8 Answers 8

12

check ob_start() function

ob_start();
echo 'some_output';
$content = ob_get_contents();
ob_end_clean();

echo 'Content generated :'.$content;
Sign up to request clarification or add additional context in comments.

Comments

5

You can get URLs like that using URL rewriting. Eg: for apache, see mod_rewrite

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

You don't actually need to be creating the files. You could create the files, but its more complicated as you need to decide when to update them if the data changes.

2 Comments

But depending on how awesome or lame your host is, you may or may not have access to URL rewriting...I know that with my 1&1 Beginner package, I don't :(
Very old post here, but I want to set the record straight. 1&1 Beginner packages do allow for mod_rewrite--at least since 2007, when I first signed up with 1&1.
3

Manual caching (creating the HTML and saving it to a file) may not be the most efficient way, but if you want to go down that path I recommend the following (ripped from a simple test app I wrote to do this):

$cache_filename = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING'];
$cache_limit_in_mins = 60 * 32; // this forms 32hrs
// check if we have a cached file already
if ( file_exists($cache_filename) )
{
    $secs_in_min = 60;
    $diff_in_secs = (time() - ($secs_in_min * $cache_limit_in_mins)) - filemtime($cache_filename);
    // check if the cached file is older than our limit
    if ( $diff_in_secs < 0 )
    {
        // it isn't, so display it to the user and stop
        print file_get_contents($cache_filename);
        exit();
    }
}

// create an array to hold your HTML output, this is where you generate your HTML
$output = array();
$output[] = '<table>';
$output[] = '<tr>';
// etc

//  Save the output as manual cache
$file = fopen ( $cache_filename, 'w' );
fwrite ( $file, implode($output,'') );
fclose ( $file );

print implode($output,'');

1 Comment

If you use setlocale in your code then this needs to be taken into account when creating the cache filename - otherwise you'll end up serving the same version to all locales.
3

In my opinion this is the best solution. I use this for cache JSON file for my Android App. It can be simply use in other PHP files. It's optimize file size from ~1mb to ~163kb (gzip).

enter image description here

Create cache folder in your directory

Then Create cache_start.php file and paste this code

<?php
header("HTTP/1.1 200 OK");
//header("Content-Type: application/json"); 
header("Content-Encoding: gzip");

$cache_filename = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING'];
$cache_filename = "./cache/".md5($cache_filename);
$cache_limit_in_mins = 60 * 60; // It's one hour


if (file_exists($cache_filename))
{
    $secs_in_min = 60;
    $diff_in_secs = (time() - ($secs_in_min * $cache_limit_in_mins)) - filemtime($cache_filename);
    if ( $diff_in_secs < 0 )
    {
        print gzencode( file_get_contents($cache_filename) );
        exit();
    }
}
ob_start("ob_gzhandler");
?>

Create cache_end.php and paste this code

<?php
$content = ob_get_contents();
ob_end_clean();
$file = fopen ( $cache_filename, 'w' );
fwrite ( $file, $content );
fclose ( $file );
echo gzencode( $content );
?>

Then create for example index.php (file which you want to cache)

<?php
include "cache_start.php";
echo "Hello Compress Cache World!";
include "cache_end.php";
?>

1 Comment

If you use setlocale in your code then this needs to be taken into account when creating the cache filename - otherwise you'll end up serving the same version to all locales.
1

I use APC for all my PHP caching (on an Apache server)

Comments

0

If you're not opposed to frameworks, try using the Zend Frameworks's Zend_Cache. It's pretty flexible, and (unlike some of the framework modules) easy to implement.

Comments

0

Can use Cache_lite from PEAR:

Details here http://mahtonu.wordpress.com/2009/09/25/cache-php-output-for-high-traffic-websites-pear-cache_lite/

Comments

0

I was thinking from the point of load on the database, and charges for data bandwidth and speed of loading. I have some pages which are unlikely to change in years, (I know it is easy to use a CMS system based on a database ). Unlike in US, here the cost of bandwidth can be high. Anybody has any views on that, whether to create htmal pages or dynamic (php, asp.net) Links to the pages would be stored on a database anyway.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.