Wednesday 6 July 2011

Detecting Browser with php

  “If you have raced with men on foot  and they have worn you out,  how can you compete with horses? If you stumble in safe country,    how will you manage in the thickets (foods) by the Jordan? "
-Jeremiah 12:5
Ever had problems with rendering your web apps on different browsers and find some inconsistencies in the display? Now this is what I have been through in the recent past. Much of my functions done in AJAX don't work with IE but perform very well with other browsers.

Most writers say that the problem comes when instantiating the object whereas you use ActiveX objects in IE the other browsers use XMLhttpRequest object.

For IE :
var request = new ActiveXObject("Microsoft.XMLHTTP");
 
For others (Safari, Google chrome and Firefox):
var request = new XMLHttpRequest();
Now I have gone through this several occasions and every time something new crops up and make a very crucial decision for my clients to use other browsers other than IE due to its non standardization and their quest to be unique at the expense of my (developer's) time doing two sets of codes for different browsers.

This problem drove me to seek to detect the browser On-page-load and give information to the user on the browser to use. Though using php to do this slows the process other than the client side JavaScript I like the php idea more.

Since I have tested most of my codes with several browsers that are commonly used and the fact that web browser market share for IE is declining see I  chose to detect the IE usage and give information to the user and provide link for download of some of the browsers;

Here is the code:
 <?php
function isie_broswer()
{
    //Gets user's operating system, as well as their browser
   $u_agent = $_SERVER['HTTP_USER_AGENT'];
   //Search the string of the operating system and browser returned to find if its IE
   if(preg_match('/MSIE/i',$u_agent))
    {
       return true;
    }
   else
   {
       return false;
   }
  
}

if(!isie_broswer())
{.....}
else
    {
   ?>
    <div align="center" style="padding-top:20px; padding-bottom:20px;">
    <table height="40" width="80%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FF3333"  class="aligncenter">
      <tr >       
        <td bgcolor="#FFFFFF" class="aligncenter" valign="middle" style="padding-top:10px;">
         <img src="images/icons/exclamation.jpg" width="100" height="100" align="left" >                                      
        This page is not designed for Intenet Explorer.  If you want to see this webpage as intended, please use a standards compliant browser, such as <a href="http://www.google.com/chrome">Google Chrome</a> .OR <a href="http://www.mozilla.com">Mozilla Firefox</a>
     </tr>
    </table>
    </div>
<?php
}
?>

I hope this helps friends like me;

Good day

No comments:

Post a Comment