저는 간단한 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; }