How to Detect Browser in PHP and JavaScript

Do you want to detect a user’s browser programmatically? Sometimes you need to show different flows or content on the basis of the browser being used. In this article, I show you how to detect browsers in PHP and JavaScript.

One of our readers asked to write an article on this topic. They wanted to provide a different flow to the end-users depending on the browser. I will show how we can get the browser name by using both programming languages – PHP and JavaScript. Users can pick any one of them.

Detect Browser in PHP

PHP provides a global variable called $_SERVER which prints information about a server and execution environment. If you print $_SERVER['HTTP_USER_AGENT'], you can see it also gives browser information. In my case, this variable gives the following output on the Chrome browser.

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

This output will be different on each browser. We need to write some logic to print the actual browser name from this output. Let’s write the PHP code for it.

<?php
$arr_browsers = ["Opera", "Edg", "Chrome", "Safari", "Firefox", "MSIE", "Trident"];
 
$agent = $_SERVER['HTTP_USER_AGENT'];
 
$user_browser = '';
foreach ($arr_browsers as $browser) {
    if (strpos($agent, $browser) !== false) {
        $user_browser = $browser;
        break;
    }   
}
 
switch ($user_browser) {
    case 'MSIE':
        $user_browser = 'Internet Explorer';
        break;
 
    case 'Trident':
        $user_browser = 'Internet Explorer';
        break;
 
    case 'Edg':
        $user_browser = 'Microsoft Edge';
        break;
}
 
echo "You are using ".$user_browser." browser";

In the variable $arr_browsers, you can see the last 2 values are “MSIE, Trident”. These values are for detecting Internet Explorer browsers. Some versions of Internet Explorer give any of these values.

Detect Users Browser in JavaScript

To detect browsers in JavaScript we use the same logic as in PHP. Just instead of $_SERVER['HTTP_USER_AGENT'] I use navigator.userAgent and follow the JavaScript coding style. The navigator.userAgent is a read-only property of JavaScript and it returns the user agent string of the current browser.

Below is the code to detect a user’s browser using JavaScript.

<script>
var browsers = ["Opera", "Edg", "Chrome", "Safari", "Firefox", "MSIE", "Trident"];
var userbrowser, useragent = navigator.userAgent;
for (var i = 0; i < browsers.length; i++) {
    if( useragent.indexOf(browsers[i]) > -1 ) {
        userbrowser = browsers[i];
        break;
    }
};
 
switch(userbrowser) {
    case 'MSIE':
        userbrowser = 'Internet Explorer';
        break;
 
    case 'Trident':
        userbrowser = 'Internet Explorer';
        break;
 
    case 'Edg':
        userbrowser = 'Microsoft Edge';
        break;
}
 
alert('You are using ' + userbrowser + ' browser');
</script>

That’s it! Using these codes you can easily detect the user’s browser and apply your condition based on the browser.

Are you using any other way to detect the browser? Let me know in the comment section below. You would also like to check out our article How to Detect Mobile Device in PHP.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

10 thoughts on “How to Detect Browser in PHP and JavaScript

  1. I tried the long php code from a couple of popular stack overflow articles, but I still couldn’t get MS Edge to actually be MS Edge instead of Chrome. However, this code got me there. Thank you.

  2. Hello, it works for me in PHP, and Im using this conditions after this script. The system recognize web browser but wthatever browser I use it still works with first condition. Please can you help me? Here is my code:

    Dátum zákroku / Date of procedure

    Dátum zákroku / Date of procedure [YYYY-MM-DD]

  3. This is great!! Specially for those who do not know how to make [script].
    I have a question: How can I use (part of) this script to only show a pop-up when Internet Explorer is used.
    Thanks very much!!

  4. This works, but ‘Opera’ should be used as “OPR” now, and the “switch” should include it like so:
    case ‘OPR’:
    $user_browser = ‘Opera’;
    break;

Leave a Reply

Your email address will not be published. Required fields are marked *