Home >Backend Development >PHP Tutorial >How Can I Detect the User's Operating System Using PHP?

How Can I Detect the User's Operating System Using PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-08 00:11:16317browse

How Can I Detect the User's Operating System Using PHP?

Get Operating System Information

In PHP, you can use the get_browser() function to retrieve information about the user's browser and operating system. The function returns an array containing the following information:

  • browser: The name of the user's browser, such as "Chrome" or "Firefox".
  • version: The version of the user's browser, such as "10.0" or "52.0".
  • platform: The platform of the user's operating system, such as "Windows" or "Linux".
  • javascript: A boolean value indicating whether JavaScript is enabled in the user's browser.

To retrieve the user's operating system information, you can use the platform element of the array returned by the get_browser() function. For example:

$browser = get_browser();
echo $browser['platform'];

Output:

Windows

Example code:

<?php

// Get user agent from header
$userAgent = $_SERVER['HTTP_USER_AGENT'];

// Sniff the operating system
$os = null;
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.2/i', $userAgent)) {
    $os = 'Windows XP x64';
} elseif (preg_match('/windows nt 5.1/i', $userAgent)) {
    $os = 'Windows XP';
} elseif (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 me/i', $userAgent)) {
    $os = 'Windows ME';
} elseif (preg_match('/win98/i', $userAgent)) {
    $os = 'Windows 98';
} elseif (preg_match('/win95/i', $userAgent)) {
    $os = 'Windows 95';
} elseif (preg_match('/win16/i', $userAgent)) {
    $os = 'Windows 3.11';
} elseif (preg_match('/macintosh|mac os x/i', $userAgent)) {
    $os = 'Mac OS X';
} elseif (preg_match('/mac_powerpc/i', $userAgent)) {
    $os = 'Mac OS 9';
} elseif (preg_match('/linux/i', $userAgent)) {
    $os = 'Linux';
} elseif (preg_match('/ubuntu/i', $userAgent)) {
    $os = 'Ubuntu';
} elseif (preg_match('/iphone/i', $userAgent)) {
    $os = 'iPhone';
} elseif (preg_match('/ipod/i', $userAgent)) {
    $os = 'iPod';
} elseif (preg_match('/ipad/i', $userAgent)) {
    $os = 'iPad';
} elseif (preg_match('/android/i', $userAgent)) {
    $os = 'Android';
} elseif (preg_match('/blackberry/i', $userAgent)) {
    $os = 'BlackBerry';
} elseif (preg_match('/webos/i', $userAgent)) {
    $os = 'Mobile';
} else {
    $os = 'Unknown';
}

// Print the operating system
echo $os;

?>

The above is the detailed content of How Can I Detect the User's Operating System Using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn