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