Caching HTML Output Using PHP

I would like to create a cache for my php pages on my site. I found too many solutions, but I want a script that can generate an HTML page from my ex database:

I have a page for categories that captures all categories from the database, so the script should be able to generate an HTML sorting page: my-categories.html. then if I select a category, I should get the page my-x-category.html, etc. etc. for other categories and subcategories.

I see that some websites have URLs, for example: wwww.the-web-site.com/the-page-ex.html

although they are dynamic.

thanks for the help

+3
source share
8 answers

check ob_start () function

ob_start(); echo 'some_output'; $content = ob_get_contents(); ob_end_clean(); echo 'Content generated :'.$content; 
+7
source

You can get such URLs using URL rewriting. For example: for apache see Mod_rewrite

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

In fact, you do not need to create files. You can create files, but this is harder because you need to decide when to update them if the data changes.

+4
source

Manual caching (creating HTML and saving it in a file) may not be the most efficient way, but if you want to go this route, I recommend the following (taken out of a simple test application that I wrote for 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_final,'') ); fclose ( $file ); print implode($output_final,''); 
+2
source

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

+1
source

If you don't mind frameworks, try using Zend Frameworks Zend_Cache . It is quite flexible and (unlike some infrastructure modules) is easily implemented.

0
source

I thought from the point of loading the database and charged a fee for bandwidth and download speed. I have several pages that are unlikely to change over the years (I know that it is easy to use a database-based CMS). Unlike the United States, bandwidth costs can be high here. Does anyone have any views on this, whether it is necessary to create htmal pages or dynamic (php, asp.net) In any case, links to pages will be stored in the database.

0
source

In my opinion, this is the best solution. I use this file for the JSON cache for my Android application. It can simply be used in other PHP files. Optimizes file size from ~ 1 to ~ 163kb (gzip) .

enter image description here

Create a 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 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 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 (the file you want to cache)

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

All Articles