PHP判断iPhone、iPad、Android、PC设备的方法

原创
2016-06-08 17:19:59 784浏览
因为工作需要我们需要知道是什么样了用户访问了我网站了,现在的移动设备种类多了,下面我们一起来看小编整理的一段PHP判断iPhone、iPad、Android、PC设备的例子。

我将使用Windows系统的设备定为PC,毕竟博客面向中国用户,大部分家用设备还是用的Windows系统

原理是判断浏览器提交的USER AGENT

代码如下 复制代码

//获取USER AGENT
$agent = strtolower($_SERVER['HTTP_USER_AGENT']);

//分析数据
$is_pc = (strpos($agent, 'windows nt')) ? true : false;
$is_iphone = (strpos($agent, 'iphone')) ? true : false;
$is_ipad = (strpos($agent, 'ipad')) ? true : false;
$is_android = (strpos($agent, 'android')) ? true : false;

//输出数据
if($is_pc){
echo "这是PC";
}
if($is_iphone){
echo "这是iPhone";
}
if($is_ipad){
echo "这是iPad";
}
if($is_android){
echo "这是Android";
}
?>

如果你只判断是否为iphone设备可以如下来进行操作

代码如下 复制代码

function get_device_type(){
$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
$type = 'other';
if(strpos($agent, 'iphone') || strpos($agent, 'ipad') ){
$type = 'ios';
}
if(strpos($agent, 'android')){
$type = 'android';
}
return $type;
}

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:避免PHP-FPM内存泄漏导致内存耗尽 下一条:Laravel调试利器Laravel Debugbar使用教程

相关文章

查看更多