在PHP中,我们可以根据“$SERVER['HTTP_USER_AGENT']”获得用户的代理信息,但是任需要进一步判断设备类型。以下是简单的判断:
<?php
$agent = "";
$useragent = strtolower($_SERVER["HTTP_USER_AGENT"]);
$is_mac = strripos($useragent,'mac');
$is_pc = strripos($useragent,'windows');
$is_android = strripos($useragent,'android');
$is_iphone = strripos($useragent,'iphone');
$is_mobile = strripos($useragent,'mobile');
$is_phone = strripos($useragent,'phone');
$is_ipad = strripos($useragent,'ipad');
$is_ipod = strripos($useragent,'ipod');
$is_wp = strripos($useragent,'windows phone');
if($is_android||$is_iphone||$is_mobile||$is_phone||$is_ipad||$is_ipod||$is_wp){
echo "设备类型:Phone/Tablet";
}else{
echo "设备类型:PC";
}
echo "<br />设备信息:".$useragent;
?>
当然,这样的判断并不是100%准确,还有一些设备没有加入判断。