Skip to content

Instantly share code, notes, and snippets.

@w-jerome
Last active March 27, 2019 08:41
Show Gist options
  • Save w-jerome/8f77915ffda8a18cd36eb791c83f4cd0 to your computer and use it in GitHub Desktop.
Save w-jerome/8f77915ffda8a18cd36eb791c83f4cd0 to your computer and use it in GitHub Desktop.
Javascript and PHP — Detect Browser
var browser = (navigator.userAgent.indexOf("Chrome") > -1) ? 'chrome' : (navigator.userAgent.indexOf("Safari") > -1) ? 'safari' : (navigator.userAgent.indexOf("Opera") > -1) ? 'opera' : (navigator.userAgent.indexOf("Firefox") > -1) ? 'firefox' : (navigator.userAgent.indexOf("MSIE") > -1 || navigator.userAgent.indexOf("Trident") > -1) ? 'ie' : 'unknow';
function getBrowser() {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (is_int(strpos($user_agent, 'Chrome'))) {
return 'chrome';
} else if (is_int(strpos($user_agent, 'Safari'))) {
return 'safari';
} else if (is_int(strpos($user_agent, 'Opera'))) {
return 'opera';
} else if (is_int(strpos($user_agent, 'Firefox'))) {
return 'firefox';
} else if (is_int(strpos($user_agent, 'MSIE')) || is_int(strpos($user_agent, 'Trident'))) {
return 'ie';
} else {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment