I couldn’t find any decent code for detecting the correct browser properly, most detected IE/Edge as Chrome, so here’s mine:

PHP

function getBrowser() {
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = $u_agent;
$browsers = ["MSIE", "Trident", "Edge", "Firefox", "Chrome", "Safari", "Opera"];
foreach ($browsers as $browser) {
if (strpos($u_agent, $browser) !== false) {
$bname = $browser;
break;
}
}
return $bname;
}

JS

function getBrowser() {
var browsers = ["MSIE", "Trident", "Edge", "Firefox", "Chrome", "Safari", "Opera"];
var userbrowser, useragent = navigator.userAgent;
for (var i = 0; i < browsers.length; i++) {
if(useragent.indexOf(browsers[i]) > -1) {
userbrowser = browsers[i];
break;
}
};
return userbrowser;
}

0 Comments

Leave a Reply

Avatar placeholder

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Verified by MonsterInsights