Thursday, January 13, 2011

Detect Internet Explorer Version using PHP and JavaScript


1. Using PHP:

<?php
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
function ae_detect_ie()
{
   $browser = $_SERVER['HTTP_USER_AGENT'];
  // echo $browser.'<BR>';
 // yank the version from the string
// $browser = substr("$browser", 25, 8);
    $browsertype = substr("$browser", 25, 8);
//    echo $browsertype;
    $browserversion_str = substr("$browser", 30, 3);
    $browserversion_float = floatval($browserversion_str);
//    echo $browserversion_str;
    if (isset($_SERVER['HTTP_USER_AGENT']) &&
    (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)
    && ($browserversion_float < 9.0)
    )
        return true;
    else
        return false;
}
?>

<!-- Using function ae_detect_ie  -->

<?php
 if (ae_detect_ie()) {
   $browser = $_SERVER['HTTP_USER_AGENT'];
    $browsertype = substr("$browser", 25, 8);
    echo '<div style="border: 2px solid #FF0000; width: 100%;  overflow: auto;">';
    echo "<font color='red'> Please note: </font> This site requires Internet Explorer 8.0 or higher. ";
    echo "It seems that your are using $browsertype.";
    echo "  Please update your Internet Explorer to 8.0 or higher. Please follow the links below to upgrade your browser.";
    echo '<BR /> Click <a href="http://www.microsoft.com/canada/windows/internet-explorer/">HERE</a> to upgrade your Internet Explorer. ';
    echo 'You can also use <a href="http://www.mozilla.com/firefox/">Firefox</a> instead.';
    echo "</div>";
//    exit(0);
    }
?>
2. Using Javascript:

<html>
<body>

<script type="text/javascript">

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function checkVersion()
{
  var msg = "You're not using Internet Explorer.";
  var ver = getInternetExplorerVersion();

  if ( ver > -1 )
  {
    if ( ver >= 8.0 )
      msg = "You're using a recent copy of Internet Explorer."
    else
      msg = "You should upgrade your copy of Internet Explorer.";
  }
  alert( msg );
}
checkVersion();
</script>

</body>
</html>

No comments:

Post a Comment