source

iOS 기기에서 PHP 페이지에 액세스할 수 있는지 확인합니다.

bestscript 2022. 11. 22. 09:31

iOS 기기에서 PHP 페이지에 액세스할 수 있는지 확인합니다.

저는 간단한 PHP 웹페이지를 가지고 있는데, iPhone/iPad 또는 Web Browser에서 액세스 할 수 있는지 여부에 따라 다른 콘텐츠를 반환하고 싶습니다.내가 어떻게 그럴 수 있을까?

사용자 에이전트 사용:$_SERVER['HTTP_USER_AGENT']간단한 검출을 위해 다음 스크립트를 사용할 수 있습니다.

<?php

//Detect special conditions devices
$iPod    = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iPhone  = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$iPad    = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
$Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android");
$webOS   = stripos($_SERVER['HTTP_USER_AGENT'],"webOS");

//do something with this information
if( $iPod || $iPhone ){
    //browser reported as an iPhone/iPod touch -- do something here
}else if($iPad){
    //browser reported as an iPad -- do something here
}else if($Android){
    //browser reported as an Android device -- do something here
}else if($webOS){
    //browser reported as a webOS device -- do something here
}

?> 

사용자 디바이스에 대한 자세한 내용을 알고 싶다면 http://deviceatlas.com 또는 http://deviceatlas.com 중 하나의 솔루션을 사용하는 것이 좋습니다.

preg_match("/iPhone|Android|iPad|iPod|webOS/", $_SERVER['HTTP_USER_AGENT'], $matches);
$os = current($matches);

switch($os){
   case 'iPhone': /*do something...*/ break;
   case 'Android': /*do something...*/ break;
   case 'iPad': /*do something...*/ break;
   case 'iPod': /*do something...*/ break;
   case 'webOS': /*do something...*/ break;
}
function user_agent(){
    $iPod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
    $iPhone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    $iPad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");
    $android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
    file_put_contents('./public/upload/install_log/agent',$_SERVER['HTTP_USER_AGENT']);
    if($iPad||$iPhone||$iPod){
        return 'ios';
    }else if($android){
        return 'android';
    }else{
        return 'pc';
    }
}
$browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
function isIosDevice(){
    $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
    $iosDevice = array('iphone', 'ipod', 'ipad');
    $isIos = false;

    foreach ($iosDevice as $val) {
        if(stripos($userAgent, $val) !== false){
            $isIos = true;
            break;
        }
    }

    return $isIos;
}

모바일 디바이스를 generel로 검출하고 싶은 경우, Cake는 Request Handler-> is Mobile()을 사용하여 지원 기능을 내장하고 있습니다(http://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html#RequestHandlerComponent: is Mobile).

<?php
$iPhone = false;
$AndroidPhone = false;
$deviceType = 0;

$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
print "<br>".$ua;

if(strpos($ua,"iphone") !== false ){
    $iPhone = true;
}
if(strpos($ua,"android") !== false){

    if(strpos($_SERVER['HTTP_USER_AGENT'],"mobile")){
        $AndroidPhone = true;        
    }
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"iPad")){
    $iPad = true;
    $Tablet = true;
    $iOS = true;
}   

if($AndroidPhone==true || $iPhone==true)
{
    $deviceType = 1;
}
?>

51Degrees의 PHP 솔루션은 이를 가능하게 합니다.무료 오픈 소스 API는 https://github.com/51Degrees/Device-Detection 에서 구할 수 있습니다.하드웨어 패밀리 속성을 사용하여 iPad/iPod/iPhone 등인지 확인할 수 있습니다.

Apple의 User-Agents 특성으로 인해 초기 결과는 일반 디바이스를 반환하지만 특정 디바이스에 관심이 있는 경우 JavaScript 클라이언트 측 오버라이드를 사용하여 특정 모델을 결정할 수 있습니다.

이를 위해 Apple 디바이스(이 경우 iPhone용)라고 판단되면 다음과 같은 로직을 구현할 수 있습니다.

// iPhone model checks.
function getiPhoneModel() {
// iPhone 6 Plus
if ((window.screen.height / window.screen.width == 736 / 414) && 
(window.devicePixelRatio == 3)) {
return "iPhone 6 Plus";
}
// iPhone 6
else if ((window.screen.height / window.screen.width == 667 / 375) && 
(window.devicePixelRatio == 2)) {
return "iPhone 6";
}
// iPhone 5/5C/5S or 6 in zoom mode
else if ((window.screen.height / window.screen.width == 1.775) && 
(window.devicePixelRatio == 2)) {
return "iPhone 5, 5C, 5S or 6 (display zoom)";
}
// iPhone 4/4S
else if ((window.screen.height / window.screen.width == 1.5) && 
(window.devicePixelRatio == 2)) {
return "iPhone 4 or 4S";
}
// iPhone 1/3G/3GS
else if ((window.screen.height / window.screen.width == 1.5) && 
(window.devicePixelRatio == 1)) {
return "iPhone 1, 3G or 3GS";
} else {
return "Not an iPhone";
};
}

또는 iPad의 경우

function getiPadVersion() {
var pixelRatio = getPixelRatio();
var return_string = "Not an iPad";
if (pixelRatio == 1 ) {
return_string = "iPad 1, iPad 2, iPad Mini 1";
}
if (pixelRatio == 2) {
return_string = "iPad 3, iPad 4, iPad Air 1, iPad Air 2, iPad Mini 2, iPad 
Mini 3";
}
return return_string;
}

51Degree가 Apple 디바이스에 대해 실시한 조사에 대한 자세한 내용은 블로그 투고(https://51degrees.com/blog/device-detection-for-apple-iphone-and-ipad를 참조하십시오.

공개:나는 51도에서 일한다.

Haim Evgi의 코드에 대응하여!== false를 마지막에 추가했습니다.

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

아이폰용입니다.

<?php
  $browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
  if ($browser == true){
    $browser = 'iphone';
  }
?>

언급URL : https://stackoverflow.com/questions/6322112/check-if-php-page-is-accessed-from-an-ios-device