Check if PHP-page is accessed from an iOS device

Check if PHP-page is accessed from an iOS device

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Detecting iOS Devices in PHP: Beyond Simple User Agent Checks

Determining whether a user is accessing your application from an iPhone or iPad requires parsing the User-Agent header sent by the browser. In PHP, this information arrives via the $_SERVER['HTTP_USER_AGENT'] global variable. While modern frameworks often abstract this away, implementing the logic directly in vanilla PHP reveals the underlying mechanics of device detection and helps you avoid brittle patterns that break with every new OS update.

The most straightforward approach is to check for specific substrings within the User-Agent string. iOS devices typically include "iPhone" or "iPad" in their header. You can use the stripos function, which performs a case-insensitive search and returns the position of the first occurrence of the needle or false if not found.

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

if (stripos($userAgent, 'iPhone') !== false) {
    // Logic for iPhone
    $deviceType = 'iphone';
} elseif (stripos($userAgent, 'iPad') !== false) {
    // Logic for iPad
    $deviceType = 'ipad';
} else {
    // Logic for other devices (desktop, Android, etc.)
    $deviceType = 'other';
}

if ($deviceType === 'iphone' || $deviceType === 'ipad') {
    echo "Welcome to the iOS experience!";
} else {
    echo "Standard content rendering.";
}

However, relying solely on substring matching is fraught with pitfalls. The primary issue is that this method distinguishes between device families but not necessarily specific models or OS versions. Furthermore, User-Agent strings are not guaranteed to be accurate. Some mobile browsers, particularly older versions of Safari or custom-built apps using WebView components, may omit the "iPhone" or "iPad" identifier entirely, reporting themselves simply as "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" or similar desktop signatures to mask their true nature. This behavior is often intentional for privacy or compatibility reasons.

If your application requires a more robust solution that accounts for these inconsistencies, consider leveraging the mobiledetect library. This popular Composer package parses the User-Agent string against a comprehensive database of mobile device signatures. It returns an object populated with properties like os, device_type, and model.

composer require mobiledetect/mobiledetectlib

Once installed, you can instantiate the class to get precise data:

use Mobile_Detect;

$detector = new Mobile_Detect();

if ($detector->isMobile()) {
    if ($detector->isTablet()) {
        // Likely iPad or Android tablet
    } else {
        // Likely iPhone, Android phone, etc.
    }
}

While mobiledetect adds reliability, it introduces a dependency and requires handling the parsing logic within your application. If you are using Laravel, the trade-off shifts slightly. The framework's ecosystem encourages leveraging existing packages rather than writing raw detection code from scratch, promoting consistency across projects. For those maintaining complex enterprise applications where device-specific styling or API response variations are critical, integrating such a library ensures that edge cases—like devices spoofing desktop User-Agents—are handled gracefully.

Ultimately, the choice between manual string parsing and using a dedicated library depends on your specific tolerance for false negatives. If you need to render distinct CSS layouts or serve mobile-optimized JSON endpoints, the overhead of mobiledetect is often justified. However, for simple conditional redirects where a substring match suffices, the native PHP approach keeps dependencies minimal. Always remember that User-Agent detection is a heuristic; it should rarely be the sole factor in critical security decisions, though it remains a standard practice for UI adaptation.

Our network