-2

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 {};
BeePollen
  • 30
  • 7
  • You have to look at the user agents these different browsers send by default, and see if there is a difference to exploit. Have you looked? – ADyson Aug 03 '23 at 14:37
  • 1
    But also...why do you want to do this? Really, your backed code should not have to care if the request is coming from browser X, browser Y, an IOT device, a Postman test, a VR headset, a smart fridge, or whatever. What problem are you trying to solve with this idea? I woud guess it's potentially a workaround for some other issue which you would be better to solve directly. – ADyson Aug 03 '23 at 14:38
  • Google has the de-facto monopoly so other browsers often need to disguise themselves as Chrome. Unless you're trying to work around a bug in one specific Chrome version that doesn't affect other Chromium-based browsers, browser detection is not likely to work for you. – Álvaro González Aug 03 '23 at 14:47
  • PHP should not know about that at all. It serves the HTML. – Markus Zeller Aug 03 '23 at 16:26

2 Answers2

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.

maiorano84
  • 11,574
  • 3
  • 35
  • 48
  • Ya. Basically Chrome versions 102 and up have their "Download" notifications on top bar, while those with versions 101 and under have their "Download" notifications on the bottom left. I usually pop a message on the website notifying users where to find their downloads, but now with the latest versions, it screws up everything. Trying to find a way to detect version and give users a message based on that version. – BeePollen Aug 03 '23 at 14:49
  • @BeePollen that would be something you could do in JavaScript a bit more reliably and easily. But really it's not your job to teach users how to use their browser!! – ADyson Aug 03 '23 at 21:39
0

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.

Markus Zeller
  • 8,516
  • 2
  • 29
  • 35