Pages

Saturday, November 7, 2015

How to detect IE in php and Javascript




In PHP

  1. $chrome = 'https://www.google.com/chrome/browser/desktop';
  2. if (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) ||
  3.        (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false) ||
  4.        (strpos($_SERVER['HTTP_USER_AGENT'], "Edge") == true)) {
  5.         header('Location:'.$chrome);
  6. }
  7. else{
  8.     // goto your page
  9. }

In Javascript



  1. // detect IE ersion
  2. var IEversion = detectIE();
  3. if (IEversion !== false) {
  4.     alert('You are using IE'+IEversion);
  5.     document.location.href = "https://www.google.com/chrome/browser/desktop/";
  6. }
  7. function detectIE() {
  8.     var getAgent = window.navigator.userAgent;
  9.     var msie = getAgent.indexOf('MSIE ');
  10.     if (msie > 0) {
  11.         return parseInt(getAgent.substring(msie+5, getAgent.indexOf('.', msie)), 10);
  12.     }
  13.     var trident = getAgent.indexOf('Trident/');
  14.     if (trident > 0) {
  15.         var rv = getAgent.indexOf('rv:');
  16.         return parseInt(getAgent.substring(rv+3, getAgent.indexOf('.', rv)), 10);
  17.     }
  18.     var edge = getAgent.indexOf('Edge/');
  19.     if (edge > 0) {
  20.         return parseInt(getAgent.substring(edge+5, getAgent.indexOf('.', edge)), 10);
  21.     }
  22.     return false;
  23. }

No comments:

Post a Comment