Updated and future way to detect iOS in PHP (or server side)

I know that a similar question was asked quite often, but many of them are outdated, and a lot has changed quite often, so again I ask the community (in October 2017) how we can determine if a user is using iOS devices or not, and I I highlight iOS, I do not need any other platforms.

The reason I need detection

  • I display photos on my website, and the user can click on some of them to download the full high resolution image. On the server side, I use X-SENDFILE to push the file towards a user who works well on everything except iOS, which does not have a download function, so in the case of iOS I need to change the headers and present the image as an image, and than the application, so that the user could click on it and save it in the gallery manually.

I can use user agent sniffing (as in the past):

$iPod    = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iPhone  = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$iPad    = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");

(, Facebook, Twitter ..), , , , . , imho.

, - iOS?

, , , , , , .

.

, , , , ?

+4
2

, , - , . , , / . , , , - . , , .

" " - , -, . Safari "()" , - . Google Analytics, - Chrome - Chrome.

-, , iOS - Safari, Android - Chrome.

, , :

//For iOS

if ((strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile/') !== false) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari/') == false) {
    echo 'WebView';
} else{
    echo 'Not WebView';
}

//For Android

if ($_SERVER['HTTP_X_REQUESTED_WITH'] == "com.company.app") {
    echo 'WebView';
} else{
    echo 'Not WebView';
}

, , , .

- :

var userAgent = window.navigator.userAgent.toLowerCase(),
safari = /safari/.test( userAgent ),
ios = /iphone|ipod|ipad/.test( userAgent );

if( ios ) {
    if ( safari ) {
        //browser
    } else if ( !safari ) {
        //webview
    };
} else {
    //not iOS
};

, , , .

0

, , X-SENDFILE (, , iOS, ), Content-Disposition: Attachment .

X-SENDFILE , , , .

<?php
function streamfile($filepath, $buffer_size) {
    $buffer = '';
    $handle = fopen($filepath, 'rb');

    if ($handle === false) {
        return false;
    }

    while (!feof($handle)) {
        $buffer = fread($handle, $buffer_size);
        echo $buffer;
        ob_flush();
        flush();
    }

    return fclose($handle);
}

$filepath = 'path/to/your/file';
$filename = 'Name of My File';
$mimetype = 'mime/type';

header('Content-Disposition: Attachment; filename="'. $filename .'"');
header('Content-Type: '.$mimetype );
header('Content-Length: '.filesize($filepath));

streamfile($filepath, 4096);
?>

, , fsockopen fopen.

0

Source: https://habr.com/ru/post/1688215/


All Articles