Hello.
Recently I want to know about user's details. SO I use these code for geting browser name, version and operating system name.
$userAgent = $_SERVER['HTTP_USER_AGENT'];
function getBrowserVersion($userAgent, $browserName) {
$pattern = "";
switch ($browserName) {
case "Internet Explorer":
$pattern = '/MSIE|Trident\/.*rv:([\d\.]+)/';
break;
case "Mozilla Firefox":
$pattern = '/Firefox\/([\d\.]+)/';
break;
case "Microsoft Edge":
$pattern = '/Edge\/([\d\.]+)/';
break;
case "Google Chrome":
$pattern = '/Chrome\/([\d\.]+)/';
break;
case "Apple Safari":
$pattern = '/Version\/([\d\.]+)/';
break;
case "Opera":
$pattern = '/Opera\/([\d\.]+)/';
break;
}
if (preg_match($pattern, $userAgent, $matches)) {
return $matches[1];
}
return "Unknown";
}
function getBrowserInfo($userAgent) {
if (preg_match('/MSIE|Trident/i', $userAgent)) {
return "Internet Explorer";
} elseif (preg_match('/Firefox/i', $userAgent)) {
return "Mozilla Firefox";
} elseif (preg_match('/Edge/i', $userAgent)) {
return "Microsoft Edge";
} elseif (preg_match('/Chrome/i', $userAgent)) {
return "Google Chrome";
} elseif (preg_match('/Safari/i', $userAgent)) {
return "Apple Safari";
} elseif (preg_match('/Opera/i', $userAgent)) {
return "Opera";
}
return "Unknown";
}
function getOperatingSystem($userAgent) {
$os = "Unknown";
if (preg_match('/Windows NT 10/i', $userAgent)) {
$os = "Windows 10";
} elseif (preg_match('/Windows NT 6.3/i', $userAgent)) {
$os = "Windows 8.1";
} elseif (preg_match('/Windows NT 6.2/i', $userAgent)) {
$os = "Windows 8";
} elseif (preg_match('/Windows NT 6.1/i', $userAgent)) {
$os = "Windows 7";
} elseif (preg_match('/Windows NT 6.0/i', $userAgent)) {
$os = "Windows Vista";
} elseif (preg_match('/Windows NT 5.1/i', $userAgent) || preg_match('/Windows XP/i', $userAgent)) {
$os = "Windows XP";
} elseif (preg_match('/Windows NT 5.0/i', $userAgent)) {
$os = "Windows 2000";
} elseif (preg_match('/Windows NT 4.0/i', $userAgent)) {
$os = "Windows NT 4.0";
} elseif (preg_match('/Macintosh|Mac OS X/i', $userAgent)) {
$os = "Mac OS X";
} elseif (preg_match('/Linux/i', $userAgent)) {
$os = "Linux";
} elseif (preg_match('/Unix/i', $userAgent)) {
$os = "Unix";
} elseif (preg_match('/Ubuntu/i', $userAgent)) {
$os = "Ubuntu";
}
return $os;
}
$operatingSystem = getOperatingSystem($userAgent);
$browserName = getBrowserInfo($userAgent);
$browserVersion = getBrowserVersion($userAgent, $browserName);
echo "Browser Name: " . $browserName . "<br>";
echo "Browser Version: " . $browserVersion;
echo "<br>Operating System: " . $operatingSystem;