Monday, August 8, 2011

How to Optimize a Web Site for a Faster Loading - Part 2

In this article I am going to describe two main techniques which will make your website more faster. Please note that I assume you are using an apache web server.

1. Enable Gzip.
2. Enable caching for static contents.



Enable Gzip
As I am a Joomla fan, first I would like to describe how to enable Gzip in Joomla. Before doing this make sure that you have already installed "mod_gzip" extension in apache.

  • Login to your Joomla administration section and go to the ''Global Configurations".


  • In Global Configurations, click on "server" and change "Gzip page compression" to Yes.

And now lets see how to enable Gzip compression using .htaccess. Please make sure that your hosting provider has enabled "mod_gzip" module or if you have your own server, check whether you have already installed and enabled this module for apache.

If everything ok, just put the below code in your .htaccess file.

  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  mod_gzip_item_include handler ^cgi-script$
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

If you don't have a .htaccess file, create one and upload it to your web root.

Enable caching for static contents
Now lets see how to enable caching for static contents. How this caching really helps you to make your website faster?

When a visitor visits first time, your page will send several HTTP requests to download all the contents. By setting expire headers in the .htaccess you can make some of those static files (images, js, css) cachable and this will reduce HTTP requests in subsequent page views. In other words your site will be loaded in subsequent visits.

To set the expires headers add these lines to your .htaccess file


  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType text/html "access plus 1 weeks"
  ExpiresByType image/gif "access plus 3 weeks"
  ExpiresByType image/jpeg "access plus 3 weeks"
  ExpiresByType image/png "access plus 3 weeks"
  ExpiresByType text/css "access plus 3 weeks"
  ExpiresByType text/javascript "access plus 2 weeks"
  ExpiresByType application/x-javascript "access plus 2 weeks"

More information about this can be found at http://httpd.apache.org/docs/2.0/mod/mod_expires.html

Finally lets see how to enable caching in the Joomla administration section. As before go to the "Global Configuration" and this time click on "system" tab.

Then find the "Cache Settings" and change the Cache to Yes as below.


Tuesday, May 17, 2011

How to Optimize a Web Site for Fast Loading - Part 1

This is a one of most common issue we have. Imagine if your site takes more than 10 seconds to load or start render the contents, actually it is a long time and really bad which will directly affect your targeted traffic. Would you wait with a cup of coffee and watching your screen until a web page finished loading ? You never and same your visitors. So let us discuss about some of interesting tips on How to make a web page optimized to load in a short time. A short loading time = A Happy visitor.

In the very step I am going to share some useful online tools where you can have ideas about your site on what are the things I should fix in my site.

1. Page Speed tool from Google labs.
This is actually a very good tool, you can find it here http://pagespeed.googlelabs.com. Just enter your site URL and it will give you a page speed score and a lot of important suggestions on where and what are the points you should improve on optimizing the site.

2. Web Page Test
This is also a very good tool and my favourite one too. Find it here http://www.webpagetest.org/. With this tool you can test your site, how it will load in different parts in the world and different browsers. This tool will give you a great report of your site optimization as well as an optimization checklist. Just give it a try.

3. Google Webmaster Tools
The Google's webmaster tools is a power tool you can use to have details of your site. If you haven't a google's webmaster account yet, I strongly suggest you to open one, enter your site there and verify. Wait for few days until Google collect information about your site. Then you can use those details to optimize and as well make SEO of your site.

The tools I mentioned above are just free. So give them a try.

Once you have understood how and where you should get your hands dirty on your site to make it optimize to gain more targeted traffic, you can start working with a clear plan.

Wednesday, January 12, 2011

Handling File Uploads with PHP

Lets have a look how to handle a file upload using PHP.
First we need to create a form to upload our file as below.
Allowing people to upload files to your web site is putting you in risk, unless you do not check what they are uploading. In this example we are checking the file size and the file type of the uploaded file. Below PHP is going to do the rest for you. The uploaded files are going to save in a folder "upload". Rename it with your folder name.

$imgName = $_FILES["file"]["name"];

if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < $_POST['MAX_IMG_SIZE']))  {
  if ($_FILES["file"]["error"] > 0)
    {
 echo "File upload Error.";
 echo "Return Code: " . $_FILES["file"]["error"];
    }
  else
    {
    if (file_exists("../upload/".$imgName))
      {
       echo imgName . " already exists. ";
      }
    else
      {
       move_uploaded_file($_FILES["file"]["tmp_name"],"../upload/" . $imgName);
       echo "File stored in: " . "upload/" . $_FILES["file"]["name"];
 
      }
    }
  }
else
  {
   echo "Invalid file type or file size too large";
  }