Home / PHP / How to Detect the browser in PHP

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 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 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()
{
$ExactBrowserNameUA=$_SERVER['HTTP_USER_AGENT'];
if (strpos(strtolower($ExactBrowserNameUA), "safari/") and strpos(strtolower($ExactBrowserNameUA), "opr/")) {
    // OPERA
    $ExactBrowserNameBR="Opera";
} elseIf (strpos(strtolower($ExactBrowserNameUA), "safari/") and strpos(strtolower($ExactBrowserNameUA), "chrome/")) {
    // CHROME
    $ExactBrowserNameBR="Chrome";
} elseIf (strpos(strtolower($ExactBrowserNameUA), "msie")) {
    // INTERNET EXPLORER
    $ExactBrowserNameBR="Internet Explorer";
} elseIf (strpos(strtolower($ExactBrowserNameUA), "firefox/")) {
    // FIREFOX
    $ExactBrowserNameBR="Firefox";
} elseIf (strpos(strtolower($ExactBrowserNameUA), "safari/") and strpos(strtolower($ExactBrowserNameUA), "opr/")==false and strpos(strtolower($ExactBrowserNameUA), "chrome/")==false) {
    // SAFARI
    $ExactBrowserNameBR="Safari";
} else {
    // OUT OF DATA
    $ExactBrowserNameBR="OUT OF DATA";
};
return $ExactBrowserNameBR;

 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 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 providing me the following output in the chrome browser. This following code snippet will define what happens whole 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;
        break;
    }   
}
 switch ($user_browser) {
    case 'MSIE':
        $user_browser = 'Internet Explorer';
        break;
 case 'Trident':
        $user_browser = 'Internet Explorer';
        break;
 case 'Edge':
        $user_browser = 'Internet Explorer';
        break;
}
 echo "You are using ".$user_browser." browser";

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 value.

Get the file from internet (at the same time of writing the exact site will navigates 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 the Unix users), at that point exposed 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.ini

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.

Browsers:
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.

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

Installations

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

As follow:

require_once('BrowserDetection.php');
   $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.';
}

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 best user interface along with the best experience for all the precious customers. be brave and expert with browser detect PHP.

Get New Freebies To Your Inbox

Get New Freebies To Your Inbox

Join our mailing list to receive the latest FREEBIES and updates from our team.

You have Successfully Subscribed!

Leave a Reply

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

Pin It on Pinterest

Share This