use HTTP::BrowserDetect; my $browser = HTTP::BrowserDetect->new($user_agent_string); # Detect operating system if ($browser->windows) { if ($browser->winnt) ... if ($browser->win95) ... } print $browser->mac; # Detect browser vendor and version print $browser->netscape; print $browser->ie; if (browser->public_major(4)) { if ($browser->public_minor() > .5) { ... } } if ($browser->public_version() > 4) { ...; }
The HTTP::BrowserDetect object does a number of tests on an HTTP user agent string. The results of these tests are available via methods of the object.
This module is based upon the JavaScript browser detection code available at http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html.
HTTP::BrowserDetect->new( $user_agent_string )
The constructor may be called with a user agent string specified. Otherwise, it will use the value specified by $ENV{'HTTP_USER_AGENT'}, which is set by the web server when calling a CGI script.
You may also use a non-object-oriented interface. For each method, you may call HTTP::BrowserDetect::method_name(). You will then be working with a default HTTP::BrowserDetect object that is created behind the scenes.
Returns the value of the user agent string.
Calling this method with a parameter has now been deprecated and this feature will be removed in an upcoming release.
Returns the country string as it may be found in the user agent string. This will be in the form of an upper case 2 character code. ie: US, DE, etc
Returns the language string as it is found in the user agent string. This will be in the form of an upper case 2 character code. ie: EN, DE, etc
Returns the method name of the actual hardware, if it can be detected. Currently returns one of: android, audrey, blackberry, dsi, iopener, ipad, iphone, ipod, kindle, n3ds, palm, ps3, psp, wap, webos. Returns undef
if no hardware can be detected
Returns a human formatted version of the hardware device name. These names are subject to change and are really meant for display purposes. You should use the device() method in your logic. Returns one of: Android, Audrey, BlackBerry, Nintendo DSi, iopener, iPad, iPhone, iPod, Amazon Kindle, Nintendo 3DS, Palm, Sony PlayStation 3, Sony Playstation Portable, WAP capable phone, webOS. Also Windows-based smartphones will output various different names like HTC T7575. Returns undef
if this is not a device or if no device name can be detected.
Returns a list of the browser properties, that is, all of the tests that passed for the provided user_agent string. Operating systems, devices, browser names, mobile and robots are all browser properties.
Please note that that the version(), major() and minor() methods have been superceded as of release 1.07 of this module. They are not yet deprecated, but should be replaced with public_version(), public_major() and public_minor() in new development.
The reasoning behind this is that version() method will, in the case of Safari, return the Safari/XXX numbers even when Version/XXX numbers are present in the UserAgent string. Because this behaviour has been in place for so long, some clients may have come to rely upon it. So, it has been retained in the interest of "bugwards compatibility", but in almost all cases, the numbers returned by public_version(), public_major() and public_minor() will be what you are looking for.
Returns the browser version as a floating-point number.
Returns the integer portion of the browser version.
Returns the decimal portion of the browser version as a floating-point number less than 1. For example, if the version is 4.05, this method returns .05; if the version is 4.5, this method returns .5.
On occasion a version may have more than one decimal point, such as 'Wget/1.4.5'. The minor version does not include the second decimal point, or any further digits or decimals.
This is probably not what you want. Please use either public_version() or engine_version() instead.
Returns the version as a floating-point number. If passed a parameter, returns true if it is equal to the version specified by the user agent string.
This is probably not what you want. Please use either public_major() or engine_major() instead.
Returns the integer portion of the browser version. If passed a parameter, returns true if it equals the browser major version.
This is probably not what you want. Please use either public_minor() or engine_minor() instead.
Returns the decimal portion of the browser version as a floating-point number less than 1. For example, if the version is 4.05, this method returns .05; if the version is 4.5, this method returns .5. This is a change in behavior from previous versions of this module, which returned a string.
If passed a parameter, returns true if equals the minor version.
On occasion a version may have more than one decimal point, such as 'Wget/1.4.5'. The minor version does not include the second decimal point, or any further digits or decimals.
Returns any the beta version, consisting of any non-numeric characters after the version number. For instance, if the user agent string is 'Mozilla/4.0 (compatible; MSIE 5.0b2; Windows NT)', returns 'b2'. If passed a parameter, returns true if equal to the beta version. If the beta starts with a dot, it is thrown away.
Returns one of the following:
Gecko, KHTML, Trident, MSIE, NetFront
Returns undef
if no string can be found.
Returns the version number of the rendering engine. Currently this only returns a version number for Gecko and Trident. Returns undef
for all other engines. The output is simply engine_major
added with engine_minor
.
Returns the major version number of the rendering engine. Currently this only returns a version number for Gecko and Trident. Returns undef
for all other engines.
Returns the minor version number of the rendering engine. Currently this only returns a version number for Gecko and Trident. Returns undef
for all other engines.
The following methods are available, each returning a true or false value. Some methods also test for the operating system version. The indentations below show the hierarchy of tests (for example, win2k is considered a type of winnt, which is a type of win32)
win16 win3x win31 win32 winme win95 win98 winnt win2k winxp win2k3 winvista win7 win8 win8_0 win8_1 wince winphone winphone7 winphone7_5 winphone8
mac68k macppc macosx ios
sun sun4 sun5 suni86 irix irix5 irix6 hpux hpux9 hpux10 aix aix1 aix2 aix3 aix4 linux sco unixware mpras reliant dec sinix freebsd bsd
It may not be possibile to detect Win98 in Netscape 4.x and earlier. On Opera 3.0, the userAgent string includes "Windows 95/NT4" on all Win32, so you can't distinguish between Win95 and WinNT.
Returns one of the following strings, or undef. This method exists solely for compatibility with the HTTP::Headers::UserAgent module.
Win95, Win98, WinNT, Win2K, WinXP, Win2k3, WinVista, Win7, Win8, Win8.1, Windows Phone, Mac, Mac OS X, iOS, Win3x, OS2, Unix, Linux, Chrome OS, Firefox OS, Playstation 3 GameOS, Playstation Portable GameOS, RIM Tablet OS, BlackBerry 10
The following methods are available, each returning a true or false value. Some methods also test for the browser version, saving you from checking the version separately.
The ie_compat_mode is used to determine if the IE user agent is for the compatibility mode view, in which case the real version of IE is higher than that detected. The true version of IE can be inferred from the version of Trident in the engine_version method.
The realplayer method above tests for the presence of either the RealPlayer plug-in "(r1 " or the browser "RealPlayer". To preserve "bugwards compatibility" and prevent false reporting, browser_string calls this method which ignores the "(r1 " plug-in signature.
Netscape 6, even though its called six, in the User-Agent string has version number 5. The nav6 and nav6up methods correctly handle this quirk. The Firefox test correctly detects the older-named versions of the browser (Phoenix, Firebird).
Returns undef on failure. Otherwise returns one of the following:
Netscape, Firefox, Safari, Chrome, MSIE, WebTV, AOL Browser, Opera, Mosaic, Lynx, Links, ELinks, RealPlayer, IceWeasel, curl, puf, NetFront, Mobile Safari, BlackBerry.
If a Gecko rendering engine is used (as in Mozilla or Firefox), returns the version of the renderer (e.g. 1.3a, 1.7, 1.8) This might be more useful than the particular browser name or version when correcting for quirks in different versions of this rendering engine. If no Gecko browser is being used, or the version number can't be detected, returns undef.
The following methods are available, each returning a true or false value.
Returns true if the browser appears to belong to a handheld device.
Returns true if the browser appears to belong to a tablet device.
Returns true if the user agent appears to be a robot, spider, crawler, or other automated Web client.
The following additional methods are available, each returning a true or false value. This is by no means a complete list of robots that exist on the Web.
Lee Semel, lee@semel.net (Original Author)
Peter Walsham (co-maintainer)
Olaf Alders, olaf at wundercounter.com
(co-maintainer)
Thanks to the following for their contributions:
cho45
Leonardo Herrera
Denis F. Latypoff
merlynkline
Simon Waters
Toni Cebrin
Florian Merges
david.hilton.p
Steve Purkis
Andrew McGregor
Robin Smidsrod
Richard Noble
Josh Ritter
Mike Clarke
Marc Sebastian Pelzer
Alexey Surikov
Maros Kollar
Jay Rifkin
Luke Saunders
Jacob Rask
Heiko Weber
Jon Jensen
Jesse Thompson
Graham Barr
Enrico Sorcinelli
Olivier Bilodeau
Yoshiki Kurihara
Paul Findlay
Uwe Voelker
Douglas Christopher Wilson
John Oatis
Atsushi Kato
Ronald J. Kimball
Bill Rhodes
Thom Blake
Aran Deltac
yeahoffline
David Ihnen
Hao Wu
Perlover
The _engine()
method currently only handles Gecko and Trident. It needs to be expanded to handle other rendering engines.
POD coverage is also not 100%.
"Browser ID (User-Agent) Strings", http://www.zytrax.com/tech/web/browser_ids.htm
You can find documentation for this module with the perldoc command.
perldoc HTTP::BrowserDetect
You can also look for information at:
The biggest limitation at this point is the test suite, which really needs to have many more UserAgent strings to test against.
Patches are certainly welcome, with many thanks for the excellent contributions which have already been received. The preferred method of patching would be to fork the GitHub repo and then send me a pull request, but plain old patch files are also welcome.
If you're able to add test cases, this will speed up the time to release your changes. Just edit t/useragents.json so that the test coverage includes any changes you have made. Please contact me if you have any questions.
This distribution uses Dist::Zilla. If you're not familiar with this module, please see https://github.com/oalders/http-browserdetect/issues/5 for some helpful tips to get you started.