How To Detect Browser In PHP And JavaScript

Do you want to detect a user’s browser programmatically? Sometimes you need to show different flow or message 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 different design/flow to the end-users depending on the browser their users are using. For this tutorial, I will show how we can get the browser by using both programming languages – PHP and JavaScript. So users can make a choice between them.

Detect Browser in PHP

PHP provides a global variable $_SERVER which print server and execution environment information. If you print $_SERVER['HTTP_USER_AGENT'], you can see it 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/76.0.3809.132 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 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 giving 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 JavaScript coding style. The navigator.userAgent is a read-only property of JavaScript and it returns the user agent string for 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 the code above 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.

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

8 thoughts on “How To Detect Browser In PHP And JavaScript

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

  2. 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!!

  3. 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 *