![]() |
It will load your page content from html cahced files. |
Copy and Paste following code very first of your page. Which Page You want to create cache.
Example:
PHP
<?php
$file = ''.$_SERVER['HTTP_HOST'].''.$_SERVER['REQUEST_URI'].'';
$file = str_replace('/', '-', $file);
$cachefile = 'cached/'.$file.'.html';
$cachetime = 18000;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
readfile($cachefile);
exit;
}
ob_start(); // Start the output buffer
?>
Copy and Paste following code very last of your page. Which Page You want to create cache.
Example:
PHP
<?php
// Cache the contents to a cache file
$cached = fopen($cachefile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); // Send the output to the browser
?>
Here is full codes.
Example:
PHP
<?php
$file = ''.$_SERVER['HTTP_HOST'].''.$_SERVER['REQUEST_URI'].'';
$file = str_replace('/', '-', $file);
$cachefile = 'cached/'.$file.'.html';
$cachetime = 18000;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
readfile($cachefile);
exit;
}
ob_start(); // Start the output buffer
?>
HTML Contents
<?php
// Cache the contents to a cache file
$cached = fopen($cachefile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); // Send the output to the browser
?>
How to create PHP Simple HTML Cache for a Web Page | PHP Cache Tutorial |
How to minify all code of Your Website | PHP Cache Tutorial |
Full Code Example of PHP Simple Cache | PHP Cache Tutorial |
Read Full: | PHP Cache Tutorial |
Category: | Web Tutorial |
Sub Category: | PHP Cache Tutorial |
Uploaded by: | Admin |
Views: | 115 |