Skip to content

Instantly share code, notes, and snippets.

@halillusion
Last active March 11, 2021 13:19
Show Gist options
  • Save halillusion/53e7484bfdc0e43ba604543f94f56de6 to your computer and use it in GitHub Desktop.
Save halillusion/53e7484bfdc0e43ba604543f94f56de6 to your computer and use it in GitHub Desktop.
PHP Device Dedector
function userAgentDetails($ua)
{
$ua = $ua==null ? $_SERVER['HTTP_USER_AGENT'] : $ua;
$browser = '';
$platform = '';
$b_icon = 'mdi mdi-close-circle';
$p_icon = 'mdi mdi-close-circle';
$browser_list = [
'Trident\/7.0' => ['Internet Explorer 11','mdi mdi-internet-explorer'],
'MSIE' => ['Internet Explorer','mdi mdi-internet-explorer'],
'Edge' => ['Microsoft Edge','mdi mdi-microsoft-edge-legacy'],
'Edg' => ['Microsoft Edge','mdi mdi-microsoft-edge'],
'Internet Explorer' => ['Internet Explorer','mdi mdi-internet-explorer'],
'Beamrise' => ['Beamrise','mdi mdi-earth'],
'Opera' => ['Opera','mdi mdi-opera'],
'OPR' => ['Opera','mdi mdi-opera'],
'Vivaldi' => ['Vivaldi','mdi mdi-earth'],
'Shiira' => ['Shiira','mdi mdi-earth'],
'Chimera' => ['Chimera','mdi mdi-earth'],
'Phoenix' => ['Phoenix','mdi mdi-earth'],
'Firebird' => ['Firebird','mdi mdi-earth'],
'Camino' => ['Camino','mdi mdi-earth'],
'Netscape' => ['Netscape','mdi mdi-earth'],
'OmniWeb' => ['OmniWeb','mdi mdi-earth'],
'Konqueror' => ['Konqueror','mdi mdi-earth'],
'icab' => ['iCab','mdi mdi-earth'],
'Lynx' => ['Lynx','mdi mdi-earth'],
'Links' => ['Links','mdi mdi-earth'],
'hotjava' => ['HotJava','mdi mdi-earth'],
'amaya' => ['Amaya','mdi mdi-earth'],
'MiuiBrowser' => ['MIUI Browser','mdi mdi-earth'],
'IBrowse' => ['IBrowse','mdi mdi-earth'],
'iTunes' => ['iTunes','mdi mdi-earth'],
'Silk' => ['Silk','mdi mdi-earth'],
'Dillo' => ['Dillo','mdi mdi-earth'],
'Maxthon' => ['Maxthon','mdi mdi-earth'],
'Arora' => ['Arora','mdi mdi-earth'],
'Galeon' => ['Galeon','mdi mdi-earth'],
'Iceape' => ['Iceape','mdi mdi-earth'],
'Iceweasel' => ['Iceweasel','mdi mdi-earth'],
'Midori' => ['Midori','mdi mdi-earth'],
'QupZilla' => ['QupZilla','mdi mdi-earth'],
'Namoroka' => ['Namoroka','mdi mdi-earth'],
'NetSurf' => ['NetSurf','mdi mdi-earth'],
'BOLT' => ['BOLT','mdi mdi-earth'],
'EudoraWeb' => ['EudoraWeb','mdi mdi-earth'],
'shadowfox' => ['ShadowFox','mdi mdi-earth'],
'Swiftfox' => ['Swiftfox','mdi mdi-earth'],
'Uzbl' => ['Uzbl','mdi mdi-earth'],
'UCBrowser' => ['UCBrowser','mdi mdi-earth'],
'Kindle' => ['Kindle','mdi mdi-earth'],
'wOSBrowser' => ['wOSBrowser','mdi mdi-earth'],
'Epiphany' => ['Epiphany','mdi mdi-earth'],
'SeaMonkey' => ['SeaMonkey','mdi mdi-earth'],
'Avant Browser' => ['Avant Browser','mdi mdi-earth'],
'Chrome' => ['Google Chrome','mdi mdi-google-chrome'],
'CriOS' => ['Google Chrome','mdi mdi-google-chrome'],
'Safari' => ['Safari','mdi mdi-apple-safari'],
'Firefox' => ['Firefox','mdi mdi-firefox'],
'Mozilla' => ['Mozilla','mdi mdi-firefox']
];
$platform_list = [
'windows' => ['Windows','mdi mdi-microsoft-windows'],
'iPad' => ['iPad','mdi mdi-apple'],
'iPod' => ['iPod','mdi mdi-apple'],
'iPhone' => ['iPhone','mdi mdi-apple'],
'mac' => ['Apple MacOS','mdi mdi-apple'],
'android' => ['Android','mdi mdi-android'],
'linux' => ['Linux','mdi mdi-linux'],
'Nokia' => ['Nokia','mdi mdi-microsoft'],
'BlackBerry' => ['BlackBerry','mdi mdi-blackberry'],
'FreeBSD' => ['FreeBSD','mdi mdi-freebsd'],
'OpenBSD' => ['OpenBSD','mdi mdi-linux'],
'NetBSD' => ['NetBSD','mdi mdi-linux'],
'UNIX' => ['UNIX','mdi mdi-mouse'],
'DragonFly' => ['DragonFlyBSD','mdi mdi-linux'],
'OpenSolaris' => ['OpenSolaris','mdi mdi-linux'],
'SunOS' => ['SunOS','mdi mdi-linux'],
'OS\/2' => ['OS/2','mdi mdi-mouse'],
'BeOS' => ['BeOS','mdi mdi-mouse'],
'win' => ['Windows','mdi mdi-windows'],
'Dillo' => ['Linux','mdi mdi-linux'],
'PalmOS' => ['PalmOS','mdi mdi-mouse'],
'RebelMouse' => ['RebelMouse','mdi mdi-mouse']
];
foreach($browser_list as $pattern => $name) {
if ( preg_match("/".$pattern."/i",$ua, $match)) {
$b_icon = $name[1];
$browser = $name[0];
$known = ['Version', $pattern, 'other'];
$pattern_version = '#(?<browser>' . join('|', $known).')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
preg_match_all($pattern_version, $ua, $matches);
//if (!preg_match_all($pattern_version, $ua, $matches)) {
//}
$i = count($matches['browser']);
if ($i != 1) {
if (strripos($ua,"Version") < strripos($ua,$pattern)){
$version = @$matches['version'][0];
}
else {
$version = @$matches['version'][1];
}
}
else {
$version = @$matches['version'][0];
}
break;
}
}
foreach($platform_list as $key => $platform) {
if (stripos($ua, $key) !== false) {
$p_icon = $platform[1];
$platform = $platform[0];
break;
}
}
if ($browser=='') {
$browser = 'undetected';
}
if ($platform=='') {
$platform = 'undetected';
}
$os_array = [
'/windows nt 10/i' => 'Windows 10',
'/windows nt 6.3/i' => 'Windows 8.1',
'/windows nt 6.2/i' => 'Windows 8',
'/windows nt 6.1/i' => 'Windows 7',
'/windows nt 6.0/i' => 'Windows Vista',
'/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
'/windows nt 5.1/i' => 'Windows XP',
'/windows xp/i' => 'Windows XP',
'/windows nt 5.0/i' => 'Windows 2000',
'/windows me/i' => 'Windows ME',
'/win98/i' => 'Windows 98',
'/win95/i' => 'Windows 95',
'/win16/i' => 'Windows 3.11',
'/macintosh|mac os x/i' => 'Mac OS X',
'/mac_powerpc/i' => 'Mac OS 9',
'/linux/i' => 'Linux',
'/ubuntu/i' => 'Ubuntu',
'/iphone/i' => 'iPhone',
'/ipod/i' => 'iPod',
'/ipad/i' => 'iPad',
'/android/i' => 'Android',
'/blackberry/i' => 'BlackBerry',
'/webos/i' => 'Mobile'
];
foreach ($os_array as $regex => $value)
{
if (preg_match($regex, $ua))
{
$os_platform = $value;
}
}
$version = empty($version) ? '' : 'v'.$version;
$os_platform = isset($os_platform) === false ? 'undetected' : $os_platform;
return [
'user_agent'=> $ua, // User Agent
'browser' => $browser, // Browser Name
'version' => $version, // Version
'platform' => $platform, // Platform
'os' => $os_platform,// Platform Detail
'b_icon' => $b_icon, // Browser Icon(icon class name like from Material Design Icon)
'p_icon' => $p_icon // Platform Icon(icon class name like from Material Design Icon)
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment