Skip to content
PHP Logo

What are some best practices for accurately identifying iPhone models and iOS versions using PHP in web development projects?

Accurately identifying iPhone models and iOS versions in web development projects can be achieved by parsing the user agent string sent by the device. This information can be useful for customizing the user experience based on the device capabilities or limitations.

php
$userAgent = $_SERVER['HTTP_USER_AGENT'];

// Check for iPhone model
if (preg_match('/iPhone/', $userAgent)) {
    echo 'This is an iPhone device.';
    
    // Check for iOS version
    if (preg_match('/iPhone OS (\d+_\d+)/', $userAgent, $matches)) {
        $iOSVersion = str_replace('_', '.', $matches[1]);
        echo 'iOS version: ' . $iOSVersion;
    }
} else {
    echo 'This is not an iPhone device.';
}