SYNOPSIS
use HTTP::BrowserDetect;
my $browser = HTTP::BrowserDetect->new($user_agent_string);
# Print general information
print "Browser: $browser->browser_string\n"
if $browser->browser_string;
print "Version: $browser->public_version$browser->public_beta\n"
if $browser->public_version;
print "OS: $browser->os_string\n"
if $browser->os_string;
# Detect operating system
if ($browser->windows) {
if ($browser->winnt) ...
if ($browser->win95) ...
}
print "Mac\n" if $browser->mac;
# Detect browser vendor and version
print "Netscape\n" if $browser->netscape;
print "MSIE\n" if $browser->ie;
if (browser->public_major(4)) {
if ($browser->public_minor() > .5) {
...
}
}
if ($browser->public_version() > 4.5) {
...;
}
DESCRIPTION
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 was originally based upon the JavaScript browser detection
code available at
http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html.
CONSTRUCTOR AND STARTUP
new()
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.
SUBROUTINES/METHODS
Browser Information
browser_string()
Returns the name of the browser from among the following values:
Netscape, Firefox, Safari, Chrome, MSIE, WebTV, AOL Browser, Opera,
Mosaic, Lynx, Links, ELinks, RealPlayer, IceWeasel, curl, puf,
NetFront, Mobile Safari, BlackBerry, Obigo, Nintendo DSi, Nintendo 3DS,
StarOffice, Lotus Notes, iCab, BrowseX, Silk.
If the browser could not be recognized, returns undef.
Browser Version
Please note that that the version(), major() and minor() methods have
been deprecated as of release 1.78 of this module. They should be
replaced with public_version(), public_major(), public_minor(), and
public_beta().
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 (i.e. it will return incorrect versions
for Safari in some cases).
public_version()
Returns the browser version (major and minor) as a string.
public_major()
Returns the major part of the version as a string. For example, for
Chrome 36.0.1985.67, this returns "36".
Returns undef if no version information can be detected.
public_minor()
Returns the minor part of the version as a string. This includes the
decimal point; for example, for Chrome 36.0.1985.67, this returns ".0".
Returns undef if no version information can be detected.
public_beta()
Returns any part of the version after the major and minor version, as a
string. For example, for Chrome 36.0.1985.67, this returns ".1985.67".
The beta part of the string can contain any type of alphanumeric
characters.
Returns undef if no version information can be detected. Returns an
empty string if version information is detected but it contains only a
major and minor version with nothing following.
os_string()
Returns one of the following strings, or undef.
Win95, Win98, WinME, WinNT, Win2K, WinXP, Win2k3, WinVista, Win7, Win8,
Win8.1, Windows Phone, Mac, Mac OS X, iOS, Win3x, OS2, Unix, VMS, Linux,
Chrome OS, Firefox OS, Playstation 3 GameOS, Playstation Portable GameOS,
RIM Tablet OS, BlackBerry 10
Browser Properties
browser_properties()
Returns a list of the browser properties. Operating systems, devices,
browser names, and general tests (e.g. "mobile" and "robot") are all
browser properties.
The methods listed below are all browser properties (in addition to
being methods).
General tests
mobile()
Returns true if the browser appears to belong to a handheld device.
tablet()
Returns true if the browser appears to belong to a tablet device.
robot()
Returns true if the user agent appears to be a robot, spider, crawler,
or other automated Web client.
lib()
Returns true if the user agent appears to be a software library (e.g.
LWP, curl, wget). Generally this also implies that robot() will return
true.
OS Platform and Version
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)
windows()
win16 win3x win31
win32
winme win95 win98
winnt
win2k winxp win2k3 winvista win7
win8
win8_0 win8_1
wince
winphone
winphone7 winphone7_5 winphone8
dotnet()
chromeos()
firefoxos()
mac()
mac68k macppc macosx ios
os2()
bb10()
rimtabletos()
unix()
sun sun4 sun5 suni86 irix irix5 irix6 hpux hpux9 hpux10
aix aix1 aix2 aix3 aix4 linux sco unixware mpras reliant
dec sinix freebsd bsd
vms()
amiga()
ps3gameos()
pspgameos()
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.
Detecting Browser Vendor
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.
aol aol3 aol4 aol5 aol6
chrome
emacs
firefox
gecko
icab
ie ie3 ie4 ie4up ie5 ie5up ie55 ie55up ie6 ie7 ie8 ie9 ie10 ie11
ie_compat_mode
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.
konqueror
lotusnotes
lynx links elinks
mobile_safari
mosaic
mozilla
neoplanet neoplanet2
netfront
netscape nav2 nav3 nav4 nav4up nav45 nav45up navgold nav6 nav6up
opera opera3 opera4 opera5 opera6 opera7
realplayer
The realplayer method above tests for the presence of either the
RealPlayer plug-in "(r1 " or the browser "RealPlayer".
realplayer_browser
The realplayer_browser method tests for the presence of the RealPlayer
browser (but returns 0 for the plugin).
safari
staroffice
webtv
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).
Detecting Devices
The following methods are available, each returning a true or false
value.
android
audrey
avantgo
blackberry
dsi
iopener
iphone
ipod
ipad
kindle
n3ds
obigo
palm
webos
wap
psp
ps3
Detecting robots
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.
ahrefs
altavista
askjeeves
baidu
curl
facebook
getright
google
googleadsbot
googleadsense
googlemobile
infoseek
java
linkexchange
lwp
lycos
mj12bot
msn (same as bing)
puf
slurp
webcrawler
wget
yahoo
yandex
yandeximages
Engine properties
The following properties indicate if a particular rendering engine is
being used.
webkit
gecko
trident
presto
khtml
Other information
user_agent()
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.
country()
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
language()
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
device()
Returns the method name of the actual hardware, if it can be detected.
Currently returns one of: android, audrey, avantgo, blackberry, dsi,
iopener, ipad, iphone, ipod, kindle, n3ds, palm, ps3, psp, wap, webos.
Returns undef if no hardware can be detected
device_name()
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.
version($version)
This is probably not what you want. Please use either public_version()
or engine_version() instead.
Returns the version as a string. If passed a parameter, returns true if
it equals the browser major version.
This function returns wrong values for some Safari versions, for
compatibility with earlier code. public_version() returns correct
version numbers for Safari.
major($major)
This is probably not what you want. Please use either public_major() or
engine_major() instead.
Returns the integer portion of the browser version as a string. If
passed a parameter, returns true if it equals the browser major
version.
This function returns wrong values for some Safari versions, for
compatibility with earlier code. public_version() returns correct
version numbers for Safari.
minor($minor)
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 string.
If passed a parameter, returns true if equals the minor version.
This function returns wrong values for some Safari versions, for
compatibility with earlier code. public_version() returns correct
version numbers for Safari.
beta($beta)
This is probably not what you want. Please use public_beta() instead.
Returns the beta version, consisting of any characters after the major
and minor version number, as a string.
This function returns wrong values for some Safari versions, for
compatibility with earlier code. public_version() returns correct
version numbers for Safari.
engine_string()
Returns the name of the rendering engine, one of the following:
Gecko, WebKit, KHTML, Trident, MSIE, Presto, NetFront
Note that this returns "WebKit" for webkit based browsers (including
the Blink fork). This is a change from previous versions of this
library, which returned "KHTML" for webkit.
Returns undef otherwise.
engine_version()
Returns the version number of the rendering engine, major and minor, as
a string.
engine_major()
Returns the major version number of the rendering engine.
engine_minor()
Returns the minor version number of the rendering engine.
gecko_version()
If a Gecko rendering engine is used (as in Mozilla or Firefox), returns
the engine version. If no Gecko browser is being used, or the version
number can't be detected, returns undef.
This is an old function, preserved for compatibility.
CREDITS
Lee Semel, lee@semel.net (Original Author)
Peter Walsham (co-maintainer)
Olaf Alders, olaf at wundercounter.com (co-maintainer)
ACKNOWLEDGEMENTS
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
TO DO
POD coverage is not 100%.
SEE ALSO
"Browser ID (User-Agent) Strings",
http://www.zytrax.com/tech/web/browser_ids.htm
HTML::ParseBrowser.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc HTTP::BrowserDetect
You can also look for information at:
* GitHub Source Repository
http://github.com/oalders/http-browserdetect
* Reporting Issues
https://github.com/oalders/http-browserdetect/issues
* AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/HTTP-BrowserDetect
* CPAN Ratings
http://cpanratings.perl.org/d/HTTP-BrowserDetect
* Search CPAN
https://metacpan.org/module/HTTP::BrowserDetect
BUGS AND LIMITATIONS
The biggest limitation at this point is the test suite, which really
needs to have many more UserAgent strings to test against.
CONTRIBUTING
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.