UserAgent.me

What Does Your User Agent Say About You?

Archive

A user agent is a computer program representing a person, for example, a browser in a Web context.

Besides a browser, a user agent could be a bot scraping webpages, a download manager, or another app accessing the Web. Along with each request they make to the server, browsers include a self-identifying User-Agent HTTP header called a user agent (UA) string. This string often identifies the browser, its version number, and its host operating system.

Spam bots, download managers, and some browsers often send a fake UA string to announce themselves as a different client. This is known as user agent spoofing.

The user agent string can be accessed with JavaScript on the client side using the navigator.userAgent property.

A typical user agent string looks like this: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0".

(Source: Mozilla.org)

User Agent String

Browser Data

Php Get User Agent String

author
Elaine Sutton
• Friday, 30 July, 2021
• 12 min read

Making statements based on opinion; back them up with references or personal experience. Anonymous ¶ Follow up to Francesco R's post from 2016. His function works for most human traffic; added a few lines to cover the most common bot traffic.

user agent browser parse string useragent parser
(Source: www.phpclasses.org)

Contents

Also fixed issue with function failing to detect strings at position 0 due to strops behavior. Ruudrp at live dot NL ¶ To my surprise I found that none of the get _browser alternatives output the correct name / version combination that I was looking for using Opera or Chrome.

They either give the wrong name e.g. Safari when in fact it should be Chrome and if the UA string includes a version number as with the latest versions of Chrome and Opera the wrong number is reported. So I took bits and pieces from the various examples and combined them and added a check for version.

“;} return array('sergeant' EU_ agent,'name' name,'version' version,'platform' platform,'pattern' pattern);} UA=browser();$yourbrowser= “Your browser: ". Francesco R ¶ If you ONLY need a very fast and simple function to detect the browser name (update to May 2016):function get _browser_name($ user _ agent){ if (strops($ user _ agent, 'Opera') || strops($ user _ agent, 'Or/')) return 'Opera'; else if (strops($ user _ agent, 'Edge')) return 'Edge'; else if (strops($ user _ agent, 'Chrome')) return 'Chrome'; else if (strops($ user _ agent, 'Safari')) return 'Safari'; else if (strops($ user _ agent, 'Firefox')) return 'Firefox'; else if (strops($ user _ agent, 'MSI') || strops($ user _ agent, 'Trident/7')) return 'Internet Explorer'; return 'Other';}echo get _browser_name($_SERVER);?>This function also resolves the trouble with Edge (that contains in the user agent the string “Safari” and “Chrome”), with Chrome (contains the string “Safari”) and IE11 (that do not contain 'MSI' like all other IE versions). Note that “strops” is the fastest function to check a string (far better than “prey_match”) and Opera + Edge + Chrome + Safari + Firefox + Internet Explorer are the most used browsers today (over 97%).

The latest version of PHP has a performance fix for this function. Jeff Williams ¶ To automatically update your browscap.ini file on Linux servers, you can use this simple shell script:You can put it in the weekly cron job folder usually located in /etc/iron.weekly just don't forget to make the script executable (ch mod 775 script name).

Tim at digital dot DE ¶ Be aware that loading PHP _browscap.ini via the brows cap PHP .ini setting may consume a non-trivial amount of memory. Current versions are several MB in size (even the “lite” one) and can eat tens of MB of RAM in each PHP process.

user agent linux string strings
(Source: www.linux.org)

I’d recommend comparing your processes’ memory consumption with and without PHP _browscap.ini being loaded. If necessary, consider creating your own stripped-down copy of PHP _browscap.ini with just the browsers that are important to you.

'UserAgent' EU_ agent,'name' name,'version' version,'platform' platform,'pattern' pattern);} UA=browser();$yourbrowser= “Your browser: ". Max at expert dot DE ¶ Be aware of the fact that this function shows what a specific browser might be able to show, but NOT what the user has turned on/off. So maybe this function tells you that the browser is Abel to JavaScript even when JavaScript is turned off by the user.

If (strops($ user _ agent, 'Trident/7.0; RV:')) {content_nav = 'Trident/7.0; RV:';} else if (strops($ user _ agent, 'Trident/7')) {content_nav = 'Trident/7';} else {content_nav = 'Opera';} Good practice would be to include HTML if-statements for IE stylesheets as well as dynamically checking the browser type.

P2 at eduardoruiz dot BS ¶ BE CAREFUL WITH THIS FUNCTION! ! This function uses a lot of CPU and RAM on the whole server resources. Perhaps if you use this function a few times then no problem, but NOT if you use at any page request, or once per session. Also, this function doesn't work correctly and may returns wrong values, wildcards or empty, so it's not very useful for web statistics.

It may be better to use CURL or other methods for a request like this which have better error handling etc, but that’s not the point of this post PHP has an in setting called user _ agent which lets you specify the useragentstring when making these sorts of HTTP requests.

agent user string support keycdn
(Source: www.keycdn.com)

So, to get info about your browser, websites often examine the ‘ user agent ’, or ‘UA’ string. An user agent (UA) string is able to be used to detect what version of a specific browser is being used on a certain operating system.

Mapping UA string tokens to a more human-readable browser name for use in code is a common pattern on the web today. When mapping the new Edge token to a browser name, Microsoft recommends using a different name than the one developer used for the legacy version of Microsoft Edge to avoid accidentally applying any legacy workarounds that are not applicable to Chromium-based browsers.

When Microsoft is notified about these types of issues, website owners are contacted and informed about the updated UA. In these cases, Microsoft uses a list of UA overrides in our Beta and Stable channels to maximize compatibility for users who access these sites.

$_SERVER is an array containing information such as headers, paths, and script locations. That said, many these variables are accounted for in the CGI/1.1 specification, so you should be able to expect those.

Note that few, if any, of these will be available (or indeed have any meaning) if running PHP on the command line. ' PHP _SELF The filename of the currently executing script, relative to the document root.

agent user string
(Source: blog.mpecsinc.ca)

For instance, $_SERVER in a script at the address http://example.com/foo/bar. The __FILE__ constant contains the full path and filename of the current (i.e. included) file.

SERVER_PROTOCOL Name and revision of the information protocol via which the page was requested; e.g. HTTP/1.0 '; REQUEST_METHOD Which request method was used to access the page; e.g. GET ', ' HEAD ', POST ', PUT '. PHP's script is terminated after sending headers (it means after producing any output without output buffering) if the request method was HEAD.

DOCUMENT_ROOT The document root directory under which the current script is executing, as defined in the server's configuration file. ' Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature.

If a script is executed with the CLI, as a relative path, such as file. ' SERVER_ADMIN The value given to the SERVER_ADMIN (for Apache) directive in the web server configuration file.

For default setups, this will be 80 '; using SSL, for instance, will change this to whatever your defined secure HTTP port is. Note : Under the Apache 2, you must set UseCanonicalName = On, as well as UseCanonicalPhysicalPort = On in order to get the physical (real) port, otherwise, this value can be spoofed and it may or may not return the physical port value.

user string agent illustration modification browser
(Source: www.e-valid.com)

' SERVER_SIGNATURE String containing the server version and virtual host name which are added to server-generated pages, if enabled. ' PATH_TRANSLATED File system- (not document root-) based path to the current script, after the server has done any virtual-to-real mapping.

PATH_INFO Contains any client-provided path name information trailing the actual script filename but preceding the query string, if available. For instance, if the current script was accessed via the URL http://www.example.com/ PHP /path_info.

There are ways to develop your website to progressively enhance itself based on the availability of features rather than by targeting specific browsers. Using the user agent to detect the browser looks simple, but doing it well is, in fact, a very hard problem.

Also, experts, or people with another point of view, can give you ideas for working around the bug. If the problem seems uncommon, it's worth checking if this bug has been reported to the browser vendor via their bug tracking system (Mozilla ; WebKit ; Blink ; Opera).

Your site needs to use a specific Web feature that some browsers don't yet support, and you want to send those users to an older Website with fewer features but that you know will work. This is the worst reason to use user agent detection because odds are eventually all the other browsers will catch up.

agent string firefox change extension windows author switcher testing positive website ie
(Source: www.howtogeek.com)

Support could have been added to other browsers at any time, but this code would have continued choosing the inferior path. As the above code demonstrates, there is always a way to test browser support without user agent sniffing.

For example, in the above code snippets, using look behind in short-regexp notation (e.g. /reg/IGM) will cause a parser error in unsupported browsers. Progressive enhancement This design technique involves developing your Website in 'layers', using a bottom-up approach, starting with a simpler layer and improving the capabilities of the site in successive layers, each using more features.

Graceful degradation This is a top-down approach in which you build the best possible site using all the features you want, then tweak it to make it work on older browsers. People use user agent sniffing to detect if the users' device is touch-friendly and has a small screen, so they can optimize their website accordingly.

While user agent sniffing can sometimes detect these, not all devices are the same: some mobile devices have big screened sizes, some desktops have a small touchscreen, some people use smart TV's which are an entirely different ballgame altogether, and some people can dynamically change the width and height of their screen by flipping their tablet on its side! Rather, add in touch conveniences such as bigger, more easily clickable buttons (you can do this using CSS by increasing the font size).

Here is an example of code that increases the padding of #exampleButton to 1em on mobile devices. This effect can be easily achieved using CSS flex boxes, sometimes with floats as a partial fallback. Also try to move less relevant/important information down to the bottom and group the page's content together meaningfully.

(Source: www.youtube.com)

Although it is off-topic, perhaps the following detailed example might give you insights and ideas that persuade you to forgo user agent sniffing. The boxes can be separated into multiple columns via two equally fair method.

The second method uses a Column layout and resents all the dogs to the left and all the cats to the right. Only in this particular scenario, it is appropriate to provide no fallback for the flex boxes/multi columns, resulting in a single column of very wide boxes on old browsers.

If more people visit the webpage to see the cats, then it might be a good idea to put all the cats higher in the source code than the dogs so that more people can find what they are looking for faster on smaller screens where the content collapses down to one column. Next, always make your code dynamic. The user can flip their mobile device on its side, changing the width and height of the page.

Or, there might be some weird flip-phone-like device thing in the future where flipping it out extends the screen. The simplest way to do this is to separate all the code that moves content around based on screen size to a single function that is called when the page is loaded and at each resize event thereafter.

Also note that there is a huge difference between the media queries (max-width: 25em), not all and (min-width: 25em), and (max-width: 24.99em) : (max-width: 25em) excludes (max-width: 25em), whereas not all and (min-width: 25em) includes (max-width: 25em). Always be very deliberate about choosing the right media query and choosing the right >=, , >, or < in any corresponding JavaScript because it is very easy to get these mixed up, resulting on the website looking working right at the screen size where the layout changes.

agent user string change firefox switcher ie windows positive interesting keep things
(Source: www.howtogeek.com)

One such case is using user agent sniffing as a fallback when detecting if the device has a touch screen. Internet Explorer (on Windows) and WebKit (on iOS) are two perfect examples.

However, Internet Explorer was such a special little wasp exception prior to version 9 that it was very easy to detect the browser based upon the browser-specific features available. WebKit is a bit worse because Apple forces all the browsers on IOS to use WebKit internally, thus the user has no way to get a better more updated browser on older devices.

For example, WebKit 6 has a bug whereby when the device orientation changes, the browser might not fire MediaQueryList listeners when it should. Most browsers set the name and version in the format BrowserName/VersionNumber, with the notable exception of Internet Explorer.

So to detect Safari you have to check for the Safari string and the absence of the Chrome string, Chromium often reports itself as Chrome too or Sea monkey sometimes reports itself as Firefox. Also, pay attention not to use a simple regular expression on the BrowserName, user agents also contain strings outside the Keyword/Value syntax.

Must containment not containFirefoxFirefox/xyzSeamonkey/xyzSeamonkeySeamonkey/xyzChromeChrome/chromium/xyzChromiumChromium/xyzSafariSafari/chrome/XYZ or Chromium/safari gives two version numbers: one technical in the Safari/XYZ token, and one user -friendly in a Version/XYZ tokenizer Opera 15+ (Blink-based engine) There are five major rendering engines: Trident, Gecko, Presto, Blink, and WebKit.

agent user firefox change string browser switcher howtogeek
(Source: www.howtogeek.com)

It is therefore important to pay attention not to trigger false-positives when detecting the rendering engine. Must containGeckoGecko/xyzWebKitAppleWebKit/pay attention, WebKit browsers add a 'like Gecko' string that may trigger false positive for Gecko if the detection is not careful. PrestoOpera/XYZ Note: Presto is no longer used in Opera browser builds >= version 15 (see 'Blink')Trident Trident/Internet Explorer put this token in the comment part of the UserAgentString EdgeHTMLEdge/the non-Chromium Edge puts its engine version after the Edge/ token, not the application version.

Note: Edge HTML is no longer used in Edge browser builds >= version 79 (see 'Blink'). BlinkChrome/XYZ Most rendering engines put the version number in the RenderingEngine/VersionNumber token, with the notable exception of Gecko. The Operating System is given in most Sergeant strings (although not web-focused platforms like Firefox OS), but the format varies a lot.

They indicate the OS, but also often its version and information on the relying on hardware (32 or 64 bits, or Intel/PPC for Mac). Like in all cases, these strings may change in the future, one should use them only in conjunction with the detection of already released browsers.

A technological survey must be in place to adapt the script when new browser versions are coming out. The most common reason to perform user agent sniffing is to determine which type of device the browser runs on.

Never assume that a browser or a rendering engine only runs on one type of device. Never use the OS token to define if a browser is on mobile, tablet or desktop.

user agent string customize maxthon switcher comparing browsers
(Source: www.maxthon.com)

Internet Explorer Immobile/XYZ token in the comment. Mozilla/5.0 (compatible; MSI 9.0; Windows Phone OS 7.5; Trident/5.0; Immobile/9.0)Edge on Windows 10 Mobile/XYZ & Edge/ tokens outside the comment. Mozilla/5.0 (Windows Phone 10.0; Android 6.0.1; Xbox; Xbox One) Apple WebKit/537.36 (HTML, like Gecko) Chrome/58.0.3029.110 Mobile Safari/537.36 Edge/16.16299In summary, we recommend looking for the string “Mob” anywhere in the Sergeant to detect a mobile device. If the device is large enough that it's not marked with “Mob”, you should serve your desktop site (which, as a best practice, should support touch input anyway, as more desktop machines are appearing with touchscreens).

Other Articles You Might Be Interested In

01: Asp Net Core User Agent
02: Webbrowser Control User Agent
03: Web Browser User Agent List
Sources
1 developers.whatismybrowser.com - https://developers.whatismybrowser.com/useragents/explore/
2 deviceatlas.com - https://deviceatlas.com/blog/list-of-user-agent-strings
3 deviceatlas.com - https://deviceatlas.com/blog/mobile-browser-user-agent-strings
4 useragentstring.com - http://useragentstring.com/pages/useragentstring.php
5 webbrowsertools.com - https://webbrowsertools.com/useragent/
6 www.howtogeek.com - https://www.howtogeek.com/114937/htg-explains-whats-a-browser-user-agent/
7 www.whatismybrowser.com - https://www.whatismybrowser.com/detect/what-is-my-user-agent
8 developer.mozilla.org - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
9 en.wikipedia.org - https://en.wikipedia.org/wiki/Usage_share_of_web_browsers