首页 > 移动平台 > 详细

PHP检测终端设备是平板、手机还是电脑

时间:2015-11-17 13:00:44      阅读:280      评论:0      收藏:0      [点我收藏+]
<?php 

$ua = $_SERVER[‘HTTP_USER_AGENT‘];

function userAgent($ua){

    $iphone = strstr(strtolower($ua), ‘mobile‘); //Search for ‘mobile‘ in user-agent (iPhone have that)
    $android = strstr(strtolower($ua), ‘android‘); //Search for ‘android‘ in user-agent
    $windowsPhone = strstr(strtolower($ua), ‘phone‘); //Search for ‘phone‘ in user-agent (Windows Phone uses that)
        
    function androidTablet($ua){ //Find out if it is a tablet
        if(strstr(strtolower($ua), ‘android‘) ){//Search for android in user-agent
            if(!strstr(strtolower($ua), ‘mobile‘)){ //If there is no ‘‘mobile‘ in user-agent (Android have that on their phones, but not tablets)
                return true;
            }
        }
    }
    $androidTablet = androidTablet($ua); //Do androidTablet function
    $ipad = strstr(strtolower($ua), ‘ipad‘); //Search for iPad in user-agent
      
    if($androidTablet || $ipad){ //If it‘s a tablet (iPad / Android)
        return ‘tablet‘;
    }
    elseif($iphone && !$ipad || $android && !$androidTablet || $windowsPhone){ //If it‘s a phone and NOT a tablet
        return ‘mobile‘;
    }
    else{ //If it‘s not a mobile device
        return ‘desktop‘;
    }    
}

echo userAgent($ua);

?>

  

 

PHP检测终端设备是平板、手机还是电脑

原文:http://www.cnblogs.com/qhorse/p/4971058.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!