Sunday, November 9, 2014

How to Improve Your Website Load Time with Three Lines of Code

Want to improve your website's loading time in PHP?? There are four easy ways by which we can decrease loading time of our PHP website in a very simple manner.

How to Improve Website Load Time in PHP

Improve Website Loading Time in PHP

  • Make some HTTP requests
  • Add a far-future expires header
  • Gzip your page's components
  • Minify your JS, HTML and CSS
Here, find the PHP script which will automatically do all of the above functions.
For that, you need to call the following at the top of your page.

Top Code  :-

PHP:
require_once('class.compressor.php'); //Include the class. The full path may be required
$compressor = new compressor('css,javascript,page');

And the following at the bottom of the page:

Bottom Code :-

PHP:
$compressor->finish();

To download the class  Click Here

Basic Requirements :-

  • Make sure that your server running at least PHP4. For JS minification PHP5+ is required. 
  • You need to set the folder where you are running the class. 
Setting of the Basic Options :-

With using comma, Options can be to separate string as follows:

Plain Text :-
PHP:
$compressor = new compressor('css,javascript,page');

The string can contain 'css', 'javascript' and 'page' depending on which elements you would like to be compressed. All element contained in the string will be gzip.

Setting of the Advanced Options :-

Alternatively, an array can be passed to the class constructor with an advanced set of options. This array would set all the compression options to on:

Plain Text :-
PHP:
$compressor = new compressor(array("javascript"=>array("cachedir"=>'/minify',
"gzip"=>true,
"minify"=>true,
),
"css"=>array("cachedir"=>'/wp-content',
"gzip"=>true,
"minify"=>true,
),
"page"=>array("gzip"=>true,
"minify"=>true
)
));

Manually, We can change above array to add or remove all the given options.

Speed Gains :-

Before Compression :- Test Page with Firebug

After Compression :- Test Page with Firebug

Click Here to check the updated page 

Reference :- http://aciddrop.com/2008/01/21/boost-your-website-load-time-with-3-lines-of-code/

0 comments:

Post a Comment