Final Updated index.php file.

All bugs are fixed and working great..
Final code..