Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect "Google Chrome" as the user-agent using PHP?

Tags:

I'm interested to know whether the user-agent is "Chrome" at the server end using PHP. Is there a reliable regular expression for parsing out the user-agent string from the request header?

like image 612
Zubin Avatar asked Jun 15 '10 18:06

Zubin


People also ask

How do I find my Chrome user agent?

Google Chrome Chrome's user agent switcher is part of its Developer Tools. Open them by clicking the menu button and selecting More Tools > Developer Tools. You can also use press Ctrl+Shift+I on your keyboard.

How do I know if my browser is using Chrome PHP?

@Tuga: If the string starts with 'Chrome' , strpos() returns 0. Since 0 == false , the if code won't run, but you want it to. The function returns an actual false if the string isn't found, so you have to compare it by type using !==

How can I get browser information in PHP?

The get_browser() function in PHP is an inbuilt function that is used to tell the user about the browser's capabilities. This function looks up the user's browscap. ini file and returns the capabilities of the user's browser.

What is the user agent of Chrome?

# Chrome for Android Chrome for Android reports its UA in the following formats, depending on whether the device is a phone or a tablet. If you are parsing user agent strings using regular expressions, the following can be used to check against Chrome on Android phones and tablets: Phone pattern: 'Android' + 'Chrome/[.


1 Answers

At this point, too many browsers are pretending to be Chrome in order to ride on its popularity as well as combating abuse of browser detection for a simple match for "Chrome" to be effective anymore. I would recommend feature detection going forward, but Chrome (and WebKit/Blink in general) is notorious for lying to feature detection mechanisms as well, so even that isn't as great as it's cracked up to be anymore either.

I can only recommend staying on top of things by comparing its known UA strings with those of other browsers through third-party sites, and creating patterns from there. How you do this depends entirely on the strings themselves. Just keep in mind that due to the nature of browsers, and UA strings, there can never be a "reliable" regular expression for matching them.

In PHP, the relevant server var is $_SERVER['HTTP_USER_AGENT'].

like image 71
BoltClock Avatar answered Oct 05 '22 14:10

BoltClock