skip to Main Content

Is there a way to detect Chrome browser version using PHP?
Basically, I’m looking for a way to detect:

if Chrome version 101 and below

and

if Chrome version 102 and above. 

Is there a way to do this?

Right now I have the basics:

function get_browser_name($user_agent){
    if (strpos($user_agent, 'Chrome')) return 'Chrome';
    return 'Other';
}

    if (get_browser_name($_SERVER['HTTP_USER_AGENT']) == "Chrome") 
    {
        echo "something here";
    }
    
    else {};

2

Answers


  1. This is NOT recommended.

    There are a myriad of reasons, but the short answer is that the Browser version is a Client Side concept and should not be relevant to the Server as this can get in the way of various server-side concerns (most notably Caching).

    I’ve made this mistake in the past, and it led to the server caching the response of a user with an unsupported browser version and sending out the cached response to users with a valid browser.

    Instead, you should probably look into parsing the User Agent with Javascript and modifying your behavior based on your versioning criteria.

    My guess is that you’re probably attempting to render out a browser compatibility notification of some kind, which can be done with a combination of server-side templating and client-side detection.

    Many answers on SO touch on this subject so definitely check them out.

    Login or Signup to reply.
  2. The server should not be aware of that. You may parse on client side with the help of the navigator object in javascript.

    navigator.appVersion
    

    for example will give you a string as

    5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36
    

    In this simple case of Chrome, you can use a regex to extract the version.

    navigator.appVersion.match(/Chrome/(d+)/)[1]
    

    gives you the 115.

    The user agent may be hidden or different to $_SERVER['HTTP_USER_AGENT'] if a firewall or anti-virus blocks it or even a proxy change it. In that case you could post it from JS via AJAX.

    But all this is not recommended.

    Just output your HTML server side. If you need to know an explicit feature is supported by a special version, check can i use for compatibility.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search