Skip to content

Instantly share code, notes, and snippets.

@douglascabral
Created April 3, 2017 12:08
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save douglascabral/6ee44fc02218936d32741f7c7f7f157e to your computer and use it in GitHub Desktop.
Save douglascabral/6ee44fc02218936d32741f7c7f7f157e to your computer and use it in GitHub Desktop.
Detect IE 11 or below
if (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) || (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false)) {
//is IE 11 or below
}
@neenjaw
Copy link

neenjaw commented Jun 23, 2018

Nice snippet, thanks!

@theproj3ct
Copy link

Works thanks

@koran2019
Copy link

koran2019 commented Sep 26, 2019

Not worked on IE with touchscreen. Modify to support touchscreens:

if (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) || preg_match('~Trident/7.0(; Touch)?; rv:11.0~',$_SERVER['HTTP_USER_AGENT'])) {
//is IE 11 or below
}

@simstern
Copy link

Thanks 👍

@garethmorgans
Copy link

Thanks 👏

@ohepworthbell
Copy link

The solution by @koran2019 got me slightly closer to the issue, but I had additional LCTE string in my user string (Lenovo touchscreen laptop) as so:

~Trident/7.0; Touch; LCTE; rv:11.0~

Adding a general wildcard helped here:

$badBrowser = 
      preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) || 
      preg_match('~Trident/7.0(.*)?; rv:11.0~', $_SERVER['HTTP_USER_AGENT']);

For anyone who is still having issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment