Horje

Tips (Total 2)


# Tips-1) How to create PHP Simple HTML Cache for a Web Page

It will load your page content from html cahced files.

Step-1: Header PHP Code for Cache

Copy and Paste following code very first of your page. Which Page You want to create cache.

header.php
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
?>

Step-2: Footer PHP Code for Cache

Copy and Paste following code very last of your page. Which Page You want to create cache.

footer.php
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
?>

Full Code Example of PHP Simple Cache

Here is full codes.

index.php
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
?>

Output should be:

Full Code Example of PHP Simple Cache

# Tips-2) How to minify all code of Your Website

CSS and Javascript

Consider the following link to minify Javascript/CSS files: https://github.com/mrclay/minify

HTML

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:

How to minify all html code of Your Website

Put following code very top of your site.

index.php
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");

?>

Output should be:

How to minify all html code of Your Website

How to minify html code of Your Website

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");?>

Output should be:

How to minify html code of Your Website

How to make short all codes of your website

This work for me on php apache wordpress + w total cache + amp put it on the top of php page

index.php
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);
}
?>

Output should be:

How to make short all codes of your website


Share on: