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
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();
Share on FacebookShare on TwitterShare on LinkedInSend email
1 answer
x
x
2024 AnswerTabsTermsContact us