how to get browser info in php

Solutions on MaxInterview for how to get browser info in php by the best coders in the world

showing results for - "how to get browser info in php"
Ashley
27 Mar 2020
1function get_agent_info() {
2  $u_agent = $_SERVER['HTTP_USER_AGENT'];
3  $temp = strtolower($_SERVER['HTTP_USER_AGENT']);
4
5  $bname    = 'Unknown';
6  $platform = 'Unknown';
7  $version  = "";
8
9  // Get the platform
10  if (preg_match('/linux/i', $temp)) {
11    $platform = 'linux';
12  }
13  elseif (preg_match('/macintosh|mac os x/i', $temp)) {
14    $platform = 'mac';
15  }
16  elseif (preg_match('/windows|win32/i', $temp)) {
17    $platform = 'windows';
18  }
19
20  // Get the name of the useragent
21  if(preg_match('/msie/i',$temp) && !preg_match('/opera/i',$temp)) {
22    $bname = 'Internet Explorer';
23    $ub = "msie";
24  }
25  elseif(preg_match('/firefox/i',$temp)) {
26    $bname = 'Mozilla Firefox';
27    $ub = "firefox";
28  }
29  elseif(preg_match('/chrome/i',$temp)) {
30    $bname = 'Google Chrome';
31    $ub = "chrome";
32  }
33  elseif(preg_match('/safari/i',$temp)) {
34    $bname = 'Apple Safari';
35    $ub = "safari";
36  }
37  elseif(preg_match('/opera/i',$temp)) {
38    $bname = 'Opera';
39    $ub = "opera";
40  }
41  elseif(preg_match('/netscape/i',$temp)) {
42    $bname = 'Netscape';
43    $ub = "netscape";
44  }
45
46  $known = array('version', $ub, 'other');
47  $pattern = '#(?<browser>' . join('|', $known) .')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
48  preg_match_all($pattern, $temp, $matches);
49
50  $i = count($matches['browser']);
51  if ($i != 1) {
52    if (strripos($temp,"version") < strripos($temp,$ub)) {
53      $version = $matches['version'][0];
54    }
55    else {
56      $version = $matches['version'][1];
57    }
58  }
59  else {
60    $version = $matches['version'][0];
61  }
62
63  if ($version == null || $version == "") {
64    $version = "?";
65  }
66
67  return array(
68    'userAgent' 	=> $u_agent,
69    'browser'      	=> $bname,
70    'version'   	=> $version,
71    'platform' 		=> $platform,
72  );
73}