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

来源:互联网 发布:windows资源监视器 编辑:程序博客网 时间:2024/06/05 15:33

因为工作需要我们需要知道是什么样了用户访问了我网站了,现在的移动设备种类多了,下面我们一起来看小编整理的一段php判断iPhone、iPad、Android、PC设备的例子.

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

原理是判断浏览器提交的USER AGENT,代码如下:

01<?php
02//获取USER AGENT
03$agent strtolower($_SERVER['HTTP_USER_AGENT']);
04//分析数据
05$is_pc = (strpos($agent'windows nt')) ? true : false;   
06$is_iphone = (strpos($agent'iphone')) ? true : false;   
07$is_ipad = (strpos($agent'ipad')) ? true : false;   
08$is_android = (strpos($agent'android')) ? true : false;  
09//输出数据 
10    if($is_pc){   
11        echo "这是PC";   
12    }   
13    if($is_iphone){   
14        echo "这是iPhone";   
15    }
16    if($is_ipad){   
17        echo "这是iPad";   
18    }   
19    if($is_android){   
20        echo "这是Android";   
21    }   
22?>

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

01<?php function get_device_type(){ 
02 $agent strtolower($_SERVER['HTTP_USER_AGENT']); 
03 $type 'other'
04 if(strpos($agent'iphone') || strpos($agent'ipad')  ){ 
05  $type 'ios'
06 
07 if(strpos($agent'android')){ 
08  $type 'android'
09 
10 return $type
11} ?>
0 0
原创粉丝点击