How to Detect the browser in PHP

As we all know PHP is one of the best general-purpose scripting languages which is mostly using for serverside web development. It is rich in coding techniques along with many facilities. Utmost of the serverside developers recognized that this is the best ever language which suites for comprehensive and small projects too. This contains many classes and this Browser detection is a method which is mostly used to identify the users’ platform such as web browser, version, platform family, platform version or if it is a mobile device or tablet. With the help of this method, the web page which was made by PHP will be more responsive to the users without any interruptions.

Reliable User Browser Detection Using PHP

Do you want to detect browser you or your user is using? Well, in this article we show you how to detect browser in PHP with sample codes and definitions. You can test and execute these simple codes to find how it works and real-time example will allow you to understand the term “browser detections” in PHP. Please read further to find amazing facts.

To know more coding you might like this :

How to identify browser detection in PHP?

By using this browser detect class, we will try to detect what the user is using equipment from the HTTP_USER_AGENT string sent by the web browser.

A good way to use the class would be to gather user statistics or to report the browser and version used for informational determinations.

This HTTP_USER_AGENT will find out the exact version and platform of the users.

Below I have added sample snippets to understand the method.

How to find the exact browser and version?

There are many instances which will get you into a bit of confusion on how to find exact browser and version. It depends on the code performance and support.

But this following snippet will help you to find the correct browser easily. Here I have written some sample code simply copy and paste,

function ExactBrowserName()
if (strpos(strtolower($ExactBrowserNameUA), "safari/") and strpos(strtolower($ExactBrowserNameUA), "opr/")) {
    // OPERA
} elseIf (strpos(strtolower($ExactBrowserNameUA), "safari/") and strpos(strtolower($ExactBrowserNameUA), "chrome/")) {
    // CHROME
} elseIf (strpos(strtolower($ExactBrowserNameUA), "msie")) {
    $ExactBrowserNameBR="Internet Explorer";
} elseIf (strpos(strtolower($ExactBrowserNameUA), "firefox/")) {
    // FIREFOX
} elseIf (strpos(strtolower($ExactBrowserNameUA), "safari/") and strpos(strtolower($ExactBrowserNameUA), "opr/")==false and strpos(strtolower($ExactBrowserNameUA), "chrome/")==false) {
    // SAFARI
} else {
    // OUT OF DATA
    $ExactBrowserNameBR="OUT OF DATA";
return $ExactBrowserNameBR;
Code language: PHP (php)

 Why do you need to use?

There are several scenarios in which you want to know the user’s browser. Possible causes are:

  • You want to avoid a certain bug in a browser
  • Perfectly help to check the existence of specific feature
  • When you need to provide different HTML codes depending on the browser being used.

Overhead are approximate of the situations. You might want to detect browser of other cases. An extremely simple way to check if it is a mobile platform or web-based system. These are the mild steps to find out.

Features of browser detection class

This can be used on many sites and more than 1400+ sites have used this method to understand the users’ device and browser.

Primarily these are the natively detected browsers and platforms below I have mentioned the list.

This got amazing features and make an amazing responsive flexible system for every web-based system.

This can gain many people requirements through simple coding techniques.

How to use?

PHP provides a global variable $_SERVER which is server and execution environment pieces of information. If you try to $_SERVER['HTTP_USER_AGENT'] print, it would give browser information.

In my case, this variable provides me the following output in the Chrome browser. This following code snippet will define what happens while compiling.

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

In the above $arr_browsers variable, you would observe the last 3 values “MSIE, Trident, Edge”. These values are for detecting Internet Explorer.

In some versions of Internet Explorer, you might get any of these values.

Get the file from the internet (at the same time of writing the exact site will navigate you to another site that has the same file like this PHP-specific browscap.ini). Now you need to place it, moreover, in c:\Windows (this is for Windows users) / or in the file directory, etc. (for Unix users). At that point, open up your php.ini file.

In there, search for the line "[browscap]" then edit the subsequent line to point to your new browscap.ini file. So it will display it like this:

browscap = c:/windows/browscap.iniCode language: JavaScript (javascript)

Supported Browsers and Platforms

This supported over 1400+ of websites and 20+ web platforms and browsers. Here we have included a wide list for your further clarifications.

Android, Bingbot, BlackBerry, BlackBerry Tablet OS, Chrome 0.2 – 72+, Edge, Firebird, Firefox 0.10 – 65+, GNU IceCat, GNU IceWeasel, Googlebot, iCab, Internet Explorer 1 – 11, Internet Explorer Mobile, Konqueror, Lynx, Mozilla, MSN TV, MSNBot, Netscape, Nokia Browser, Opera 4.02 – 58+, Opera Mini, Opera Mobile, Phoenix, Safari 1 – 11+, Samsung Internet, UC Browser, W3C Validator, Yahoo! Multimedia and Yahoo! Slurp.

Android, BlackBerry, FreeBSD, iOS, Linux, Macintosh, NetBSD, Nokia, OpenBSD, OpenSolaris, Symbian, Windows, Windows CE, and Windows Phone.


To install, simply upload the BrowserDetection.php (You can find this in the lib directory) to your web host and require_once it in your PHP script.

As follows:

   $browser = new Wolfcast\BrowserDetection();
   if ($browser->getName() == 	my\BrowserDetection::BROWSER_FIREFOX &&
   	$browser->compareVersions($browser->getVersion(), '5.0') >= 0) {
   	 echo 'You are using FireFox version 5 or greater.';
}Code language: PHP (php)

Winding Up!

Browser detection is a simple class which is mostly used to identify the environment that users are using. I hope these sample code snippets will help in detecting the device.

Now you should already know that you can use the $_SERVER['HTTP_USER_AGENT'] variable to see what web browser requested a page.

Use this simple technique on your future projects to give the best user interface along with the best experience for all the precious customers. Be brave and expert with browser detect PHP.

  • A Z Hasnain Kabir

    I am an aspiring software engineer currently studying at the Islamic University of Technology in Bangladesh. My technical skills include Python automation, data science, machine learning, PHP, cURL and more. With a passion for creating innovative solutions, I am dedicated to mastering the art of software engineering and contributing to the technological advancements of tomorrow.

Leave a Comment