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
?>
Consider the following link to minify Javascript/CSS files: https://github.com/mrclay/minify
Tell Apache to deliver HTML with GZip - this generally reduces the response size by about 70%. (If you use Apache, the module configuring gzip depends on your version: Apache 1.3 uses mod_gzip while Apache 2.x uses mod_deflate.)
Accept-Encoding: gzip, deflate
Content-Encoding: gzip
Use the following snippet to remove white-spaces from the HTML with the help ob_start's buffer:
Put following code very top of your site.
Example:
PHP
<?php
function sanitize_output($buffer) {
$search = array(
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s', // shorten multiple whitespace sequences
'/<!--(.|\s)*?-->/' // Remove HTML comments
);
$replace = array(
'>',
'<',
'\\1',
''
);
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
}
ob_start("sanitize_output");
?>
Keep following code very top of your side page.
Example:
PHP
<?php
function minify_output($buffer){
$search = array('/\>[^\S ]+/s','/[^\S ]+\</s','/(\s)+/s');
$replace = array('>','<','\\1');
if (preg_match("/\<html/i",$buffer) == 1 && preg_match("/\<\/html\>/i",$buffer) == 1) {
$buffer = preg_replace($search, $replace, $buffer);
}
return $buffer;
}
ob_start("minify_output");?>
This work for me on php apache wordpress + w total cache + amp put it on the top of php page
Example:
PHP
<?Php
if(substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')){
ob_start ('ob_html_compress');
}else{
ob_start();
}
function ob_html_compress($buf) {
$search = array('/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s', '/<!--(.|\s)*?-->/');
$replace = array('>', '<', '\\1', '');
$buf = preg_replace($search, $replace, $buf);
return $buf;
return str_replace(array("\n","\r","\t"),'',$buf);
}
?>