How to make html cache in my website?


Cache is very important for your website.

It is made a site faster. It will increase visitors.

 

Follow steps.

 

Step-1

Create a top-cache.php and add following codes in top-cache.php

<?php

$url = $_SERVER[“SCRIPT_NAME”];

$break = Explode(‘/’, $url);

$file = $break[count($break) – 1];

$cachefile = ‘cached-‘.substr_replace($file ,””,-4).’.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

?>

 

Step-2

Create a bottom-cache.php and and add following codes in bottom-cache.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

?>

 

Step-3

Now, Go to your website and add following codes in this ways.

 

Add in very header

<?php

include(‘top-cache.php’);

?>

 

Add in very footer

<?php

include(‘bottom-cache.php’);

?>

 

Example of placing code:

In a php file, you can add in following way.

<?php

include(‘top-cache.php’);

?>

 

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

 

<?php

include(‘bottom-cache.php’);

?>

 

Done.

 



Find More related How to make html cache in my website?


Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *