How to detect in PHP what web browser are visitors using ?

Can you please give me suggestion, how to detect the name of web browser, that is used by visitors ? I need to use this function in PHP.
0
give a positive ratinggive a negative rating
27 Jul 2019 at 06:10 PM
Hi,

To detect the name of visitor's web browser, you can the use the function below. At first, it collects the general informations via $_SERVER['HTTP_USER_AGENT']. Then it search for a specific browser name. If there is a match, it will return browser name.

function getBrowser() {

$user_agent = $_SERVER['HTTP_USER_AGENT'];
$browser = "N/A";

$browsers = array(
'/msie/i' => 'Internet explorer',
'/firefox/i' => 'Firefox',
'/safari/i' => 'Safari',
'/chrome/i' => 'Chrome',
'/edge/i' => 'Edge',
'/opera/i' => 'Opera',
'/mobile/i' => 'Mobile browser'
);

foreach ($browsers as $regex => $value) {
if (preg_match($regex, $user_agent)) { $browser = $value; }
}

return $browser;
}

echo "Browser: " . getBrowser();
0
give a positive ratinggive a negative rating
04 Aug 2019 at 07:52 PM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
2024 AnswerTabsTermsContact us