How can PHP be used to determine the operating system and browser of a client?
To determine the operating system and browser of a client using PHP, you can utilize the $_SERVER superglobal array which contains information about the server and the client. By accessing specific keys in this array, such as 'HTTP_USER_AGENT', you can extract details about the client's operating system and browser.
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$os = '';
$browser = '';
if (strpos($user_agent, 'Windows') !== false) {
$os = 'Windows';
} elseif (strpos($user_agent, 'Macintosh') !== false) {
$os = 'Macintosh';
} elseif (strpos($user_agent, 'Linux') !== false) {
$os = 'Linux';
}
if (strpos($user_agent, 'MSIE') !== false || strpos($user_agent, 'Trident') !== false) {
$browser = 'Internet Explorer';
} elseif (strpos($user_agent, 'Firefox') !== false) {
$browser = 'Firefox';
} elseif (strpos($user_agent, 'Chrome') !== false) {
$browser = 'Chrome';
} elseif (strpos($user_agent, 'Safari') !== false) {
$browser = 'Safari';
} elseif (strpos($user_agent, 'Opera') !== false) {
$browser = 'Opera';
}
echo "Operating System: $os <br>";
echo "Browser: $browser";