Skip to content

Instantly share code, notes, and snippets.

@bkastl
Last active July 11, 2020 00:43
Show Gist options
  • Save bkastl/7022253 to your computer and use it in GitHub Desktop.
Save bkastl/7022253 to your computer and use it in GitHub Desktop.
mobile / not mobile detection via useragent
A lightweight mobile / not mobile detection.
Strings required to be recognized as mobile:
Mobi|Mini|Netfront|MOT|Maemo|Iris|Symbian|Fennec|Blazer|BOLT|GoBrowser|SEMC-Browser|Skyfire|TeaShark|Brew|uZard|Maemo|AvantGo|CHTML|PDA|MIDP|PPC|Smartphone|WebOS|PSP
Strings required to be recognized as tablet, when mobile:
iPad|Tablet|Kindle|tablet
Possible problems:
Kindle (right now, only Kindle Tablets?)
Example PHP for detecting mobile only, not tablet:
$useragent = $_SERVER['HTTP_USER_AGENT'];
$ismobile = (preg_match('/(Mobi|Mini|Netfront|MOT|Maemo|Iris|Symbian|Fennec|Blazer|BOLT|GoBrowser|SEMC-Browser|Skyfire|TeaShark|Brew|uZard|Maemo|AvantGo|CHTML|PDA|MIDP|PPC|Smartphone|WebOS|PSP)', $useragent) === 1 && preg_match('/(iPad|Tablet|Kindle)/i', $useragent) === 0);
User Agent Strings from:
http://www.useragentstring.com/pages/Mobile%20Browserlist/
http://www.zytrax.com/tech/web/mobile_ids.html
thanks to @fhemberger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment