php判断用户终端类型

来源:互联网 发布:在线教学软件 编辑:程序博客网 时间:2024/05/18 04:57

PHP简单判断iPhone、iPad、Android及PC设备的类型(将使用Windows系统的设备定为PC

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

<?php 

//获取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; 

}

0 0
原创粉丝点击