How to detect browser in php


0

In this article, we will see How to detect browser in PHP programming language. Now we are going to generate our own PHP function that we can call later to detect the browser.

We are going to use the $_SERVER global variable. The $_SERVER[‘HTTP_USER_AGENT’] can returns several details related to the current browser.

Now, given is our PHP function which we can use to display the browser:

Read Also:

<?php

function get_the_browser()
{

if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)
   return 'Internet explorer';
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Trident') !== false)
    return 'Internet explorer';
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false)
   return 'Mozilla Firefox';
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false)
   return 'Google Chrome';
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false)
   return "Opera Mini";
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== false)
   return "Opera";
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false)
   return "Safari";
 else
   return 'Other';

}


?>

In the above given code sample , we are checking respectively opportunity browser that may be and return the current browser name. Here we have not checked the Mozilla by reason of the fact that of most of the browser using this as the user agent string.

Given Below is how to display the current browser name on our web page:

echo get_the_browser();

We will competent to see the browser.

Notice that, That will always not going to give us the correct details. Server-side browser detection may sometime wrong. So, client-side browser detection is more better than server-side detection. We can also use JavaScript to detect current browser from the client side.

Also Read:


Like it? Share with your friends!

0
Developer

0 Comments