14

I use below code to find user agent,

    $user_agent = $_SERVER['HTTP_USER_AGENT']; 
    if (preg_match('/MSIE/i', $user_agent)) { 
       echo "Internet Explorer";
    }
    if (preg_match('/Firefox/i', $user_agent)) { 
       echo "FireFox";
    }
    if (strpos( $user_agent, 'Chrome') !== false)
    {
        echo "Google Chrome";
    }
    if (strpos( $user_agent, 'Safari') !== false)
    {
       echo "Safari";
    }
    if (preg_match('/Opera/i', $user_agent)) { 
       echo "Opera";
    }

    ?>

But my chrome browser returning below useragent suddenly

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.155 Safari/537.22

It contains the word safari and chrome.so both the browser names are printed.what is the solution for this.thanks.

Vishnu Vishwa
  • 210
  • 1
  • 2
  • 8
  • 3
    The solution is almost always "Stop caring about what the browser is" – Quentin Mar 07 '13 at 13:12
  • 1
    I'm guessing it is related to the fact that both browsers use the `WebKit` layout engine. You should look into feature detection as browser detection is unreliable. You should consider the idea of [progressive enhancement](http://en.wikipedia.org/wiki/Progressive_enhancement) where you create base functionality that all browsers can use and extend that functionality for browsers that support additional **features** (i.e. feature detection). – War10ck Mar 07 '13 at 13:12
  • To follow up on @Quentin comment, always ask yourself _why_ your _server_ needs to know what the browser is. I understand using _feature detection_ on the client side, but I can't understand why you need to know this on your server. – Colin M Mar 07 '13 at 13:40
  • @ColinMorelli here's an idea ... what if you wanted to dynamically include javascript that you only want to even execute (feature detection for starters) on certain browsers? So that you save a roundtrip if you don't need it (I mean, most libs let you dynamically load js anyways, but shhhhh) – jcolebrand Mar 11 '13 at 13:55
  • @jcolebrand Use feature detection on the client side. If it's supported, then load the library. Avoid the round trip unless you need it. – Colin M Mar 11 '13 at 14:06
  • @ColinMorelli *ahem* you might have noticed where I said that the common libs would let you dynamically load the js you want anyways? I specifically countered that for you. But on occasion, on first request of a page, you might want to be nicer to your clients by not asking them to download any more code than is necessary, no? – jcolebrand Mar 11 '13 at 14:11
  • @jcolebrand I noticed - I'm just saying that even without using client libraries it's not hard to implement on your own. I'm all for saving bandwidth, but you could pretty easily implement an `init.js` file that does feature detection and loads the rest of the JS from there. Just food for thought, I see where you're coming from as well I just wouldn't recommend server side browser detection. The only real case I have used it for is just to save in a sessions table (purely for informational purposes). – Colin M Mar 11 '13 at 15:52
  • @ColinMorelli I never sniff browser side but I've seen the ASP.NET framework do it. Still not entirely sure why. I was just stabbing at this one. – jcolebrand Mar 11 '13 at 16:09
  • 1
    @jcolebrand Because it's Microsoft. 'nuff said. :) – Colin M Mar 11 '13 at 16:16

2 Answers2

25

Chrome's user agent contains Safari but Safari's user agent doesn't contain Chrome so use if ... elseif:

if (stripos( $user_agent, 'Chrome') !== false)
{
    echo "Google Chrome";
}

elseif (stripos( $user_agent, 'Safari') !== false)
{
   echo "Safari";
}

Note: use stripos instead of strpos to account for case-variations.

Michael
  • 11,912
  • 6
  • 49
  • 64
  • 1
    Couldn't you also just ... invert the test? ;-) Think about it. – jcolebrand Mar 11 '13 at 13:56
  • He can't invert the test because both contain "Safari" ... So the first test passes only for Chrome... Safari is in both.. so the second check passes correctly... if the second was first both browsers would pass true the first statement. – Andrew Nov 15 '19 at 13:33
  • This will give you false positive Safari for Android browser – bksi Sep 13 '20 at 08:18
3

Try this :

$browser = get_browser(null, true);
print_r($browser);

From doc : Attempts to determine the capabilities of the user's browser, by looking up the browser's information in the browscap.ini file.

ref: http://php.net/manual/en/function.get-browser.php

Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73