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

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


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'].


Worth mentioning that if you also want to include Chrome for iOS, you will need to match against "CriOS" as well:

if (strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'CriOS') !== false) {    // User agent is Google Chrome}


Building on @Adams answer, more accurately detecting Google Chrome by exclude some browsers with "Chrome" in the user agent string using useragentstring.com and udger.com :

if(preg_match('/(Chrome|CriOS)\//i',$_SERVER['HTTP_USER_AGENT']) && !preg_match('/(Aviator|ChromePlus|coc_|Dragon|Edge|Flock|Iron|Kinza|Maxthon|MxNitro|Nichrome|OPR|Perk|Rockmelt|Seznam|Sleipnir|Spark|UBrowser|Vivaldi|WebExplorer|YaBrowser)/i',$_SERVER['HTTP_USER_AGENT'])){    // Browser might be Google Chrome}