33RIV33

Browser detection

Nov 6th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.02 KB | None | 0 0
  1. <?php
  2.  
  3. $user_agent     =   $_SERVER['HTTP_USER_AGENT'];
  4.  
  5. function getOS() {
  6.  
  7.     global $user_agent;
  8.  
  9.     $os_platform    =   "Unknown OS Platform";
  10.  
  11.     $os_array       =   array(
  12.                             '/windows nt 6.2/i'     =>  'Windows 8',
  13.                             '/windows nt 6.1/i'     =>  'Windows 7',
  14.                             '/windows nt 6.0/i'     =>  'Windows Vista',
  15.                             '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
  16.                             '/windows nt 5.1/i'     =>  'Windows XP',
  17.                             '/windows xp/i'         =>  'Windows XP',
  18.                             '/windows nt 5.0/i'     =>  'Windows 2000',
  19.                             '/windows me/i'         =>  'Windows ME',
  20.                             '/win98/i'              =>  'Windows 98',
  21.                             '/win95/i'              =>  'Windows 95',
  22.                             '/win16/i'              =>  'Windows 3.11',
  23.                             '/macintosh|mac os x/i' =>  'Mac OS X',
  24.                             '/mac_powerpc/i'        =>  'Mac OS 9',
  25.                             '/linux/i'              =>  'Linux',
  26.                             '/ubuntu/i'             =>  'Ubuntu',
  27.                             '/iphone/i'             =>  'iPhone',
  28.                             '/ipod/i'               =>  'iPod',
  29.                             '/ipad/i'               =>  'iPad',
  30.                             '/android/i'            =>  'Android',
  31.                             '/blackberry/i'         =>  'BlackBerry',
  32.                             '/webos/i'              =>  'Mobile'
  33.                         );
  34.  
  35.     foreach ($os_array as $regex => $value) {
  36.  
  37.         if (preg_match($regex, $user_agent)) {
  38.             $os_platform    =   $value;
  39.         }
  40.  
  41.     }  
  42.  
  43.     return $os_platform;
  44.  
  45. }
  46.  
  47. function getBrowser() {
  48.  
  49.     global $user_agent;
  50.  
  51.     $browser        =   "Unknown Browser";
  52.  
  53.     $browser_array  =   array(
  54.                             '/msie/i'       =>  'Internet Explorer',
  55.                             '/firefox/i'    =>  'Firefox',
  56.                             '/safari/i'     =>  'Safari',
  57.                             '/chrome/i'     =>  'Chrome',
  58.                             '/opera/i'      =>  'Opera',
  59.                             '/netscape/i'   =>  'Netscape',
  60.                             '/maxthon/i'    =>  'Maxthon',
  61.                             '/konqueror/i'  =>  'Konqueror',
  62.                             '/mobile/i'     =>  'Handheld Browser'
  63.                         );
  64.  
  65.     foreach ($browser_array as $regex => $value) {
  66.  
  67.         if (preg_match($regex, $user_agent)) {
  68.             $browser    =   $value;
  69.         }
  70.  
  71.     }
  72.  
  73.     return $browser;
  74.  
  75. }
  76.  
  77.  
  78. $user_os        =   getOS();
  79. $user_browser   =   getBrowser();
  80.  
  81. $device_details =   "<strong>Browser: </strong>".$user_browser."<br /><strong>Operating System: </strong>".$user_os."";
  82.  
  83. print_r($device_details);
  84.  
  85. echo("<br /><br /><br />".$_SERVER['HTTP_USER_AGENT']."");
  86.  
  87. ?>
Advertisement
Add Comment
Please, Sign In to add comment