HTTP::B...erDetect(3)

πŸ’… HTTP::BrowserDetect(3) β€” User Contributed Perl Documentation πŸ’…

okay bestie, welcome to the manual page for HTTP::BrowserDetect(3). It's filed under User Contributed Perl Documentation, and it's all about sniffing browser, version, and platform out of an HTTP user agent string. Let's get into it. ✨

🌸 NAME 🌸

HTTP::BrowserDetect β€” determine Web browser, version, and platform from an HTTP user agent string. πŸ’–

⭐ VERSION ⭐

This is version 3.40. Slay.

πŸŽ€ SYNOPSIS πŸŽ€

okay so here's the whole cute little flow, bestie:

use HTTP::BrowserDetect ();

my $user_agent_string
    = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36';
my $ua = HTTP::BrowserDetect->new($user_agent_string);

# Print general information
print 'Browser: ' . $ua->browser_string . "\n" if $ua->browser_string;
print 'Version: ' . $ua->browser_version . $ua->browser_beta . "\n" if $ua->browser_version;
print 'OS: ' . $ua->os_string . "\n" if $ua->os_string;

# Detect operating system
if ( $ua->windows ) {
    if ( $ua->winnt ) {
        # do something
    }
    if ( $ua->win95 ) {
        # do something
    }
}
print "Mac\n" if $ua->macosx;

# Detect browser vendor and version
print "Safari\n" if $ua->safari;
print "MSIE\n" if $ua->ie;
print "Mobile\n" if $ua->mobile;
if ( $ua->browser_major(4) ) {
    if ( $ua->browser_minor > .5 ) {
        # ...;
    }
}
if ( $ua->browser_version > 4.5 ) {
    # ...;
}

πŸ’– DESCRIPTION πŸ’–

The HTTP::BrowserDetect object runs a bunch of tests on an HTTP user agent string, and you get the results through the object's methods. It's giving detective energy. πŸ•΅οΈβ€β™€οΈ

If you want to see this module's parsing live online, check out https://www.browserdetect.org/. No cap. ✨

This module was originally based on the JavaScript browser detection code at http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html. Iconic origin story. πŸ’…

πŸš€ CONSTRUCTOR AND STARTUP πŸš€

new()

HTTP::BrowserDetect->new( $user_agent_string )

You can call the constructor with a user agent string. If you don't, it uses the value from $ENV{'HTTP_USER_AGENT'}, which the web server sets when it calls a CGI script. That's the whole startup vibe. 🫢

🧚 SUBROUTINES/METHODS 🧚

πŸ¦‹ Browser Information πŸ¦‹

browser()

Returns the browser as one of these values:

  • chrome
  • firefox
  • ie
  • opera
  • safari
  • adm
  • applecoremedia
  • blackberry
  • brave
  • browsex
  • dalvik
  • elinks
  • links
  • lynx
  • emacs
  • epiphany
  • galeon
  • konqueror
  • icab
  • lotusnotes
  • mosaic
  • mozilla
  • netfront
  • netscape
  • n3ds
  • dsi
  • obigo
  • polaris
  • pubsub
  • realplayer
  • seamonkey
  • silk
  • staroffice
  • ucbrowser
  • webtv
  • samsung

If the browser couldn't be identifiedβ€”either because it's unrecognized or because it's a robotβ€”it returns "undef". πŸ’€

browser_string()

Returns a human-formatted version of the browser name. These names are subject to change and are meant for display purposes. This may include information additional to what's in browser() (e.g. distinguishing Firefox from Iceweasel). If the user agent couldn't be identified, or if it was identified as a robot instead, returns "undef". Display purposes only, bestie. πŸŽ€

πŸ’œ Browser Version πŸ’œ

🚨 Please note: the version(), major() and minor() methods have been deprecated as of release 1.78 of this module. Replace them with browser_version(), browser_major(), browser_minor(), and browser_beta(). Periodt.
The reasoning is that version() 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).

browser_version()

Returns the browser version (major and minor) as a string. For example, for Chrome 36.0.1985.67, this returns "36.0". Slay. ✨

browser_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. 😭

browser_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". Decimal point included, iykyk.

Returns undef if no version information can be detected.

browser_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. Alphanumeric chaos welcome.

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. 🫢

πŸ“ Operating System πŸ“

os()

Returns one of these strings, or "undef":

  • windows
  • winphone
  • mac
  • macosx
  • linux
  • android
  • ios
  • os2
  • unix
  • vms
  • chromeos
  • firefoxos
  • ps3
  • psp
  • rimtabletos
  • blackberry
  • amiga
  • brew

os_string()

Returns a human-formatted version of the OS name. These names are subject to change and are really meant for display purposes. This may include information additional to what's in os() (e.g. distinguishing various editions of Windows from one another) (although for a way to do that that's more suitable for use in program logic, see below under "OS related properties"). That's a little forward reference, bestie.

Returns "undef" if no OS information could be detected.

os_version(), os_major(), os_minor(), os_beta()

Returns version information for the OS, if any could be detected. The format is the same as for the browser_version() functions. 🌈

🌈 Mobile Devices 🌈

mobile()

Returns true if the browser appears to belong to a mobile phone or similar device (i.e. one small enough that the mobile version of a page is probably preferable over the desktop version). πŸ“±

In previous versions, tablet devices sometimes had mobile() return true. They are now mutually exclusive. Mutually exclusive now, no cap. πŸ’…

tablet()

Returns true if the browser appears to belong to a tablet device. πŸ¦‹

device()

Returns the type of mobile / tablet 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, winphone.

Returns "undef" if this is not a tablet/mobile device or no hardware information can be detected. πŸ™ˆ

device_string()

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. This may include additional information (such as the model of phone if it is detectable).

Returns "undef" if this is not a portable device or if no device name can be detected. 🌸

πŸ”₯ Robots πŸ”₯

robot()

If the user agent appears to be a robot, spider, crawler, or other automated Web client, this returns one of these values:

  • lwp
  • slurp
  • yahoo
  • bingbot
  • msnmobile
  • msn
  • msoffice
  • ahrefs
  • altavista
  • apache
  • askjeeves
  • baidu
  • curl
  • facebook
  • getright
  • googleadsbot
  • googleadsense
  • googlebotimage
  • googlebotnews
  • googlebotvideo
  • googlefavicon
  • googlemobile
  • google
  • golib
  • indy
  • infoseek
  • ipsagent
  • linkchecker
  • linkexchange
  • lycos
  • malware
  • mj12bot
  • nutch
  • phplib
  • puf
  • rubylib
  • scooter
  • specialarchiver
  • wget
  • yandexbot
  • yandeximages
  • java
  • headlesschrome
  • unknown

Returns "unknown" when the user agent is believed to be a robot but is not identified as one of the above specific robots.

Returns "undef" if the user agent is not a robot or cannot be identified.

Note that if a robot crafts a user agent designed to impersonate a particular browser, we generally set properties appropriate to both the actual robot, and the browser it is impersonating. For example, googlebot-mobile pretends to be mobile safari so that it will get mobile versions of pages. In this case, browser() will return 'safari', the properties will generally be set as if for Mobile Safari, the 'robot' property will be set, and robot() will return 'googlemobile'. πŸ‘‘

lib()

Returns true if the user agent appears to be an HTTP library or tool (e.g. LWP, curl, wget, java). Generally libraries are also classified as robots, although it is impossible to tell whether they are being operated by an automated system or a human. 🧚

robot_string()

Returns a human-formatted version of the robot name. These names are subject to change and are meant for display purposes. This may include additional information (e.g. robots which return "unknown" from robot() generally can be identified in a human-readable fashion by reading robot_string() ). πŸŽ€

robot_id()

This method is currently in beta. πŸ§ͺ

Returns an id consisting of lower case letters, numbers and dashes. This id will remain constant, so you can use it for matching against a particular robot. The ids were introduced in version 3.14. There may still be a few corrections to ids in subsequent releases. Once this method becomes stable the ids will also be frozen. 🧊

all_robot_ids()

This method returns an ArrayRef of all possible robot_id values. πŸ’–

robot_version(), robot_major(), robot_minor(), robot_beta()

Returns version information for the robot, if any could be detected. The format is the same as for the browser_version() functions.

Note that if a robot crafts a user agent designed to impersonate a particular browser, we generally return results appropriate to both the actual robot, and the browser it is impersonating. For example, googlebot-mobile pretends to be mobile safari so that it will get mobile versions of pages. In this case, robot_version() will return the version of googlebot-mobile, and browser_version() will return the version of Safari that googlebot-mobile is impersonating. πŸ”₯

πŸ’– Browser Properties πŸ’–

Okay so bestie, listen β€” this is where it ALL comes together. Operating systems, devices, browser names, rendering engines, and the true-or-false methods (like mobile and lib) β€” every single one of those is a browser property. ✨

Like, say you call browser_properties() on Mobile Safari running on an Android. You get this literal list back:

('android', 'device', 'mobile', 'mobile_safari', 'safari', 'webkit')

Obsessed. It's giving "one query, all the tea." 🍡

🌸 browser_properties() 🌸

Returns all the properties for this user agent, as a list. But ngl, fair warning: because a large number of cases gotta be considered, this takes significantly more time than just querying the particular methods you actually care about. So like, don't be lazy about it if you don't have to be. πŸ’…

Here's a mostly complete list of properties (and yes β€” each one of these is both a method you can call and a property that might show up in the list returned by browser_properties() ). Plus, on top of this list, robot(), lib(), device(), mobile(), and tablet() are all browser properties too. 🫢

The following methods are all available, and each one returns a true or false value. Some of them also test for the operating system version β€” saving you a whole extra check. The indentations below show the hierarchy of tests (so for example, win2k counts as a type of winnt, which is itself a type of win32). Slay, nesting.

  • windows()
    • win16, win3x, win31
    • win32
      • winme, win95, win98
      • winnt
        • win2k, winxp, win2k3, winvista, win7
        • win8
          • win8_0, win8_1
        • win10
          • win10_0
    • wince
    • winphone
      • winphone7, winphone7_5, winphone8, winphone10
  • dotnet()
  • x11()
  • webview()
  • 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()
Heads up bestie πŸ™ˆ β€” it may not be possible to detect Win98 in Netscape 4.x and earlier. And on Opera 3.0, the userAgent string includes "Windows 95/NT4" on all Win32, so you literally can't distinguish between Win95 and WinNT. Not our fault, blame the UA string.

The following methods are available too, each returning a true or false value. Some of them also test for the browser version, which literally saves you from checking the version separately. Iconic behaviour, honestly. πŸ‘‘

  • adm
  • aol, aol3, aol4, aol5, aol6
  • applecoremedia
  • avantgo
  • browsex
  • chrome
  • dalvik
  • emacs
  • epiphany
  • firefox
  • galeon
  • icab
  • ie, ie3, ie4, ie4up, ie5, ie5up, ie55, ie55up, ie6, ie7, ie8, ie9, ie10, ie11
  • ie_compat_mode
  • konqueror
  • lotusnotes
  • lynx, links, elinks
  • mobile_safari
  • mosaic
  • mozilla
  • neoplanet, neoplanet2
  • netfront
  • netscape, nav2, nav3, nav4, nav4up, nav45, nav45up, navgold, nav6, nav6up
  • obigo
  • opera, opera3, opera4, opera5, opera6, opera7
  • polaris
  • pubsub
  • realplayer
  • realplayer_browser
  • safari
  • samsung
  • seamonkey
  • silk
  • staroffice
  • ucbrowser
  • webtv

So the ie_compat_mode one is used to figure out if the IE user agent is for the compatibility mode view β€” and if it is, the real version of IE is higher than the one that got detected. You can infer the true version of IE from the version of Trident in the engine_version method. πŸ•΅οΈβ€β™€οΈ

And the realplayer method above tests for the presence of either the RealPlayer plug-in "(r1 " or the browser "RealPlayer". Meanwhile realplayer_browser tests for the presence of the RealPlayer browser (but returns 0 for the plugin). Subtle but important, iykyk.

Fun little quirk bomb πŸ’€ β€” Netscape 6, even though it's literally called six, has version number 5 in the User-Agent string. The nav6 and nav6up methods correctly handle this quirk. And the Firefox test correctly detects the older-named versions of the browser (Phoenix, Firebird). We love a history lesson. πŸŽ€

The following methods are available, each returning a true or false value. Short and sweet, periodt.

  • android
  • audrey
  • avantgo
  • blackberry
  • dsi
  • iopener
  • iphone
  • ipod
  • ipad
  • kindle
  • kindlefire
  • n3ds
  • palm
  • webos
  • wap
  • psp
  • ps3
Important clarification, bestie 🌸 β€” 'wap' indicates that the device is capable of WAP, not necessarily that the device is limited to WAP only. Most modern WAP devices are also capable of rendering standard HTML. So don't go assuming, ok?

πŸ’€ Robot properties πŸ’€

These methods are now deprecated and will be removed in a future release. Please use the robot() and robot_id() methods to identify the bots instead. And use robot_id() if you need to match on a string, because the value that gets returned by "robot" could possibly change in a future release. Don't say we didn't warn you. πŸ™ˆ

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 (there are so many of them, it's giving endless scroll).

  • ahrefs
  • altavista
  • apache
  • askjeeves
  • baidu
  • bingbot
  • curl
  • facebook
  • getright
  • golib
  • google
  • googleadsbot
  • googleadsense
  • googlemobile
  • indy
  • infoseek
  • ipsagent
  • java
  • linkexchange
  • lwp
  • lycos
  • malware
  • mj12bot
  • msn
  • msoffice
  • puf
  • rubylib
  • slurp
  • wget
  • yahoo
  • yandex
  • yandeximages
  • headlesschrome

⭐ Engine properties ⭐

These properties tell you if a particular rendering engine is being used. Vibe check for the engine room:

  • webkit
  • gecko
  • trident
  • presto
  • khtml

🌈 Other methods 🌈

user_agent()

Returns the value of the user agent string. Easy.

Calling this method with a parameter to set the user agent has now been removed β€” so please use HTTP::BrowserDetect->new() to pass the user agent string instead. The old way is gone, bestie, we moved on. πŸ’…

u2f()

Returns true if this browser and version are known to support Universal Second Factor (U2F). Note that this method will need future updates as more browsers fully support this standard. It's a work in progress, we're all growing. 🌱

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.

engine()

Returns the rendering engine, one of the following:

gecko, webkit, khtml, trident, ie, presto, netfront

Note that this returns "webkit" for webkit based browsers (including Chrome/Blink). That's a change from previous versions of this library, which returned "KHTML" for webkit. Glow up, honestly. ✨

Returns "undef" if none of the above rendering engines can be detected. 😭

engine_string()

Returns a human formatted version of the rendering engine.

Note that this returns "WebKit" for webkit based browsers (including Chrome/Blink). This is a change from previous versions of this library, which returned "KHTML" for webkit. Same glow up, but capitalised this time. πŸ’–

Returns "undef" if none of the known rendering engines can be detected.

engine_version(), engine_major(), engine_minor(), engine_beta()

Returns version information for the rendering engine, if any could be detected. The format is the same as for the browser_version() functions. Cute and consistent. πŸŽ€


🫢 Deprecated methods 🫢

device_name()

Deprecated alternate name for device_string(). That's it. That's the whole thing. πŸ‘‹

version()

This is probably not what you want. Please use either browser_version() or engine_version() instead. Fr fr, read the label. πŸ™ˆ

Returns the version (major and minor) as a string.

This function returns wrong values for some Safari versions, for compatibility with earlier code. browser_version() returns correct version numbers for Safari. So like... use that one??

major()

This is probably not what you want. Please use either browser_major() or engine_major() instead.

Returns the integer portion of the browser version as a string.

This function returns wrong values for some Safari versions, for compatibility with earlier code. browser_version() returns correct version numbers for Safari.

minor()

This is probably not what you want. Please use either browser_minor() or engine_minor() instead.

Returns the decimal portion of the browser version as a string.

This function returns wrong values for some Safari versions, for compatibility with earlier code. browser_version() returns correct version numbers for Safari.

beta()

This is probably not what you want. Please use browser_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. browser_version() returns correct version numbers for Safari.

public_version(), public_major(), public_minor(), public_beta()

Deprecated. Please use browser_version() and related functions instead. That's the whole note, bestie. πŸ’…

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 β€” please use engine_version() in new code. It's giving "still invited to the party but doesn't get aux." 🎧


πŸ‘‘ 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 (legends, all of them):

  • 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
  • Daniel Stadie
  • ben hengst
  • Andrew Moise
  • Atsushi Kato
  • Marco Fontani
  • Nicolas Doye

πŸ¦‹ TO DO πŸ¦‹

POD coverage is not 100%. (Honest queen. We respect it. πŸ’€)

✨ SEE ALSO ✨

πŸ’– 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

<https://github.com/oalders/http-browserdetect>

  • Reporting Issues

<https://github.com/oalders/http-browserdetect/issues>

  • Search CPAN

<https://metacpan.org/module/HTTP::BrowserDetect>

🫢 CONTRIBUTING 🫢

okay so patches? yes pls β€” they are certainly welcome πŸ’– and huge thanks for the excellent contributions that have already been received, no cap. The preferred method of patching would be to fork the GitHub repo and then send a pull request. that's the move 🫢

Please include a test case too, because 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 open a GitHub issue if you have any questions. iykyk ✨


πŸ‘‘ AUTHORS πŸ‘‘

  • Lee Semel β€” lee@semel.net
  • Peter Walsham
  • Olaf Alders β€” olaf@wundercounter.com (current maintainer) πŸ’…

This software is copyright (c) 1999 by Lee Semel.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. periodt ✨

okay bestie, that's the whole page πŸ’– this one came from perl v5.42.0 and is dated 2024-03-08. mwah πŸ’‹πŸŽ€

πŸ“¦ where this page comes from