Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Identifying Browser & Platform

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 185
    Comment on it

    PHP has given some useful environment variable that are there in the phpinfo.php page that was used to setup the PHP environment.The best environment variables set by PHP is HTTP_USER_AGENT which identifies the user's browser and operating system.

    PHP has a function getenv() to access the value of all the environment variables. The information contained in the HTTP_USER_AGENT environment variable is used to create dynamic content matchable to the browser.

    ex

    1. <html>
    2. <body>
    3. <?php
    4. function getBrowser() {
    5. $u_agent = $_SERVER['HTTP_USER_AGENT'];
    6. $bname = 'Unknown';
    7. $platform = 'Unknown';
    8. $version = "";
    9. //First get the platform?
    10. if (preg_match('/linux/i', $u_agent)) {
    11. $platform = 'linux';
    12. }elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
    13. $platform = 'mac';
    14. }elseif (preg_match('/windows|win32/i', $u_agent)) {
    15. $platform = 'windows';
    16. }
    17. // Next get the name of the useragent yes seperately and for good reason
    18. if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) {
    19. $bname = 'Internet Explorer';
    20. $ub = "MSIE";
    21. } elseif(preg_match('/Firefox/i',$u_agent)) {
    22. $bname = 'Mozilla Firefox';
    23. $ub = "Firefox";
    24. } elseif(preg_match('/Chrome/i',$u_agent)) {
    25. $bname = 'Google Chrome';
    26. $ub = "Chrome";
    27. }elseif(preg_match('/Safari/i',$u_agent)) {
    28. $bname = 'Apple Safari';
    29. $ub = "Safari";
    30. }elseif(preg_match('/Opera/i',$u_agent)) {
    31. $bname = 'Opera';
    32. $ub = "Opera";
    33. }elseif(preg_match('/Netscape/i',$u_agent)) {
    34. $bname = 'Netscape';
    35. $ub = "Netscape";
    36. }
    37. // finally get the correct version number
    38. $known = array('Version', $ub, 'other');
    39. $pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
    40. if (!preg_match_all($pattern, $u_agent, $matches)) {
    41. // we have no matching number just continue
    42. }
    43. // see how many we have
    44. $i = count($matches['browser']);
    45. if ($i != 1) {
    46. //we will have two since we are not using 'other' argument yet
    47. //see if version is before or after the name
    48. if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
    49. $version= $matches['version'][0];
    50. }else {
    51. $version= $matches['version'][1];
    52. }
    53. }else {
    54. $version= $matches['version'][0];
    55. }
    56. // check if we have a number
    57. if ($version == null || $version == "") {$version = "?";}
    58. return array(
    59. 'userAgent' => $u_agent,
    60. 'name' => $bname,
    61. 'version' => $version,
    62. 'platform' => $platform,
    63. 'pattern' => $pattern
    64. );
    65. }
    66. // now try it
    67. $ua = getBrowser();
    68. $yourbrowser = "Your browser: " . $ua['name'] . " " . $ua['version'] .
    69. " on " .$ua['platform'] . " reports: <br >" . $ua['userAgent'];
    70. print_r($yourbrowser);
    71. ?>
    72. </body>
    73. </html>

    The result is this

    Your browser: Mozilla Firefox 43.0 on linux reports:
    Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: