-1

Possible Duplicate:
Determine Browser’s Version

I want to determine the browser type in the server code.

As it is, I am spending too much time hacking the HTML/CSS to get the site to display consistently across all browsers. The only browser in which the site is rendered differently (incorrectly), is IE, so I want to notify the users so that they understand why the pages are not as they would expect.

To this end, I am only interested in detecting requests from IE, so that I can inform IE users that the site is best viewed using another web browser.

I believe that the browser type is sent in one of the HTTP headers as part of the GET request. I am thinking of using this in my logic. Is there a better way?

A code snippet will be very useful. something along the lines of:

<?php
 $flag = $_GET(SOME_VARIABLE); // check if it is ANY IE version
?>
Community
  • 1
  • 1
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
  • 2
    [`get_browser()`](http://us3.php.net/manual/en/function.get-browser.php) – Michael Berkowski Dec 07 '12 at 12:06
  • Why has this been marked down?. If you mark a question down, at least, have the courtesy to explain your action, so that (if valid), it prevents similar questions from being asked again. – Homunculus Reticulli Dec 07 '12 at 12:17
  • 1
    Perhaps the downvoter is not pleased that you don't fully support IE ;) In all seriousness though, browser detection is a pretty basic standard thing that web developers need to know about. There is loads of information on the web (including [so]) dealing with browser detection. Perhaps next time you should do a quick search before - you might find that someone else has already asked this question and already received some great answers :) – Lix Dec 07 '12 at 12:20
  • 1
    Those "best viewed in XXX" is just a bad excuse nowadays; it belongs in the previous decade. – Ja͢ck Dec 07 '12 at 12:55
  • @jack: In an ideal, world, that would be true. Unfortunately, there are still many people out there using IE8 (and earlier). – Homunculus Reticulli Dec 07 '12 at 14:03

5 Answers5

3

You can take a look on $_SERVER['HTTP_USER_AGENT'] :)

Sorin Trimbitas
  • 1,467
  • 18
  • 35
2

PHP's get_browser function should be perfect for you -

get_browser - Tells what the user's browser is capable of

return values - The information is returned in an object or an array which will contain various data elements representing, for instance, the browser's major and minor version numbers and ID string; TRUE/FALSE values for features such as frames, JavaScript, and cookies; and so forth.

$browser = get_browser(null, true);

This function can return an array containing much information about the users browser

Array
(
    ...
    [platform] => WinXP
    [browser] => Firefox
    [version] => 0.9
    [majorver] => 0
    [minorver] => 9
    ...
)  
Community
  • 1
  • 1
Lix
  • 47,311
  • 12
  • 103
  • 131
  • 1
    It should be noted that for `get_browser()` to work, you need to install and keep up to date `browscap.ini`. – MrCode Dec 07 '12 at 12:11
  • Wow, I never came across this function before. Seems like what I need. I am not sure about browscap.ini though - which sounds like an extra dependency, which I am not too keen on .. – Homunculus Reticulli Dec 07 '12 at 12:13
  • @hom - For simply detecting IE versions you should be fine (and it should already work on most default setups). You'd need to update it should there be any changes in the features of the browsers and you want to detect a certain capability... – Lix Dec 07 '12 at 12:17
1

The internet is full of functions for browser detection

   function detect_ie(){
     if (isset($_SERVER['HTTP_USER_AGENT']) && 
         (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)){
           return true;
        }else{
           return false;
        }
    }
v0d1ch
  • 2,738
  • 1
  • 22
  • 27
0

if you wanna be more specific you can try.


function getBrowser()
    {
        $u_agent = $_SERVER['HTTP_USER_AGENT'];
        $bname = 'Unknown';
        $platform = 'Unknown';
        $version= "";
        if (preg_match('/linux/i', $u_agent))
        {
            $platform = 'linux';
        }
        elseif (preg_match('/macintosh|mac os x/i', $u_agent))
        {
            $platform = 'mac';
        }
        elseif (preg_match('/windows|win32/i', $u_agent))
        {
            $platform = 'windows';
        }

        if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
        {
            $bname = 'Internet Explorer';
            $ub = "MSIE";
        }
        elseif(preg_match('/Firefox/i',$u_agent))
        {
            $bname = 'Mozilla Firefox';
            $ub = "Firefox";
        }
        elseif(preg_match('/Chrome/i',$u_agent))
        {
            $bname = 'Google Chrome';
            $ub = "Chrome";
        }
        elseif(preg_match('/Safari/i',$u_agent))
        {
            $bname = 'Apple Safari';
            $ub = "Safari";
        }
        elseif(preg_match('/Opera/i',$u_agent))
        {
            $bname = 'Opera';
            $ub = "Opera";
        }
        elseif(preg_match('/Netscape/i',$u_agent))
        {
            $bname = 'Netscape';
            $ub = "Netscape";
        }

        $known = array('Version', $ub, 'other');
        $pattern = '#(?' . join('|', $known) .')[/ ]+(?[0-9.|a-zA-Z.]*)#';
        if (!preg_match_all($pattern, $u_agent, $matches))
        {

        }
        $i = count($matches['browser']);
        if ($i != 1)
        {
            if (strripos($u_agent,"Version")  $u_agent,
            'name'      => $bname,
            'version'   => $version,
            'platform'  => $platform,
            'pattern'    => $pattern
        );
    }

Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
0

this will do the trick, but you should also just do it client side with JavaScript.

Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61