32

As strange as I find this, I have not been able to find a good PHP function anywhere which will do an intelligent parse of a user agent string? Googled it for about 20 minutes now.

I have the string already, I just need something that will chop it up and give me at least browser/ver/os.

Know of a good snippet anywhere?

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Spot
  • 7,962
  • 9
  • 46
  • 55
  • Yes, I know I can write my own, but I really don't have time at the moment. I need something I can plug in and use to finish up this project. – Spot Jan 23 '10 at 10:12
  • get_browser() is quiet old and slow. Use one of the parser here http://github.com/ThaDafinser/UserAgentParser – ThaDafinser Feb 15 '16 at 09:26
  • warning against using the suggested library above (TheDafinser). It seems to work by pushing a request to another site (http://www.useragentstring.com/) which as we speak is DOWN!!!! Since I was using it somewhere, it slowed my site like nobody's business since the request had to run until timeout. Be warned – gthuo Jan 31 '17 at 15:51

5 Answers5

24

The get_browser() function has been available in PHP for quite a long a time.

The PHP manual is free, can be downloaded in various formats and viewed online (with comments)

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
symcbean
  • 47,736
  • 6
  • 59
  • 94
  • 5
    This would be a perfect example of how being up for more than two days can affect memory and cause you to ask extremely stupid questions about functions you already know about. :) Thanks – Spot Jan 23 '10 at 10:21
  • The post below (https://github.com/garetjax/phpbrowscap) is what the PHP manual suggests. – Jeremy Jun 25 '12 at 22:23
  • Is there any performance penalty when enabling `get_browser()` function in php ? – Bhavesh G Sep 20 '14 at 05:22
  • @Bhavesh: that's a very complex question. Fortunately it has a very simple answer: measure it :) – symcbean Sep 21 '14 at 22:42
  • 1
    Forget to mention that in order to use that function you need a ~10MB php_browscap.ini file. - Otherwise this error will occur: `browscap ini directive not set` –  Nov 04 '14 at 15:00
  • get_browser() is quiet old and slow. Use one of the parser here https://github.com/ThaDafinser/UserAgentParser – ThaDafinser Jan 12 '16 at 14:05
  • None of these solutions work when processing a remote request e.g. like from an API. So, given we have the user agent string (from `$_SERVER['HTTP_USER_AGENT']`), how can we parse it? – Sean H Aug 14 '17 at 08:57
  • All of these solutions are predicated on parsing `$_SERVER['HTTP_USER_AGENT']` - if the available parsers are not working then you don't have a user agent string in $_SERVER['HTTP_USER_AGENT'] – symcbean Aug 14 '17 at 09:56
12

https://github.com/browscap/browscap-php - this is a standalone library that aims to replace get_browser function. For shared hostings it seems to be a better option.

Works on: PHP 5

rap-2-h
  • 30,204
  • 37
  • 167
  • 263
MartyIX
  • 27,828
  • 29
  • 136
  • 207
3

You can try to use: https://github.com/tobie/ua-parser. This is multi-language tool to parsing user agent string. For PHP is here: https://github.com/tobie/ua-parser/tree/master/php

Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65
0

Try out ThaDafinser/UserAgentParser it's an abstraction for many available user agent parsers.

So if you are not happy with one - just switch to another (or combine them)

You can try it out here

ThaDafinser
  • 499
  • 3
  • 13
  • gave me very many issues when the website they throw the processing requests to was down. It slowed my site badly. – gthuo Feb 01 '17 at 05:30
  • Then you might used a remote parser like "http://useragentstring.com/" (which is down lately). Of course that will then slow your website down. You can use a local parser when you rely on speed. See https://github.com/ThaDafinser/UserAgentParser#local-providers – ThaDafinser Feb 01 '17 at 07:18
0

There is a PHP class library i found out, and it has worked so well for me. It is in the link below.

https://www.toms-world.org/blog/parseuseragentstring

It is lightweight and just does what I wanted (Browser, Version, OS, Mobile/PC, and much more) in a memory-friendy manner.

gthuo
  • 2,376
  • 5
  • 23
  • 30