基于php判断客户端类型
在网站用户数据统计的时候,我们经常需要获取用户浏览器的类型和操作系统的类型,本文主要介绍的是基于php判断客户端类型,有需要的朋友可以来了解一下。
方法一:
/**
*
*根据php的$_SERVER['HTTP_USER_AGENT']中各种浏览器访问时所包含各个浏览器特定的字符串来判断是属于PC还是移动端
*@authordiscuz3x
*@lastmodify2014-04-09
*@returnBOOL
*/
functioncheckmobile(){
global$_G;
$mobile=array();
//各个触控浏览器中$_SERVER['HTTP_USER_AGENT']所包含的字符串数组
static$touchbrowser_list=array('iphone','android','phone','mobile','wap','netfront','java','operamobi','operamini',
'ucweb','windowsce','symbian','series','webos','sony','blackberry','dopod','nokia','samsung',
'palmsource','xda','pieplus','meizu','midp','cldc','motorola','foma','docomo','up.browser',
'up.link','blazer','helio','hosin','huawei','novarra','coolpad','webos','techfaith','palmsource',
'alcatel','amoi','ktouch','nexian','ericsson','philips','sagem','wellcom','bunjalloo','maui','smartphone',
'iemobile','spice','bird','zte-','longcos','pantech','gionee','portalmmm','jigbrowser','hiptop',
'benq','haier','^lct','320x320','240x320','176x220');
//window手机浏览器数组【猜的】
static$mobilebrowser_list=array('windowsphone');
//wap浏览器中$_SERVER['HTTP_USER_AGENT']所包含的字符串数组
static$wmlbrowser_list=array('cect','compal','ctl','lg','nec','tcl','alcatel','ericsson','bird','daxian','dbtel','eastcom',
'pantech','dopod','philips','haier','konka','kejian','lenovo','benq','mot','soutec','nokia','sagem','sgh',
'sed','capitel','panasonic','sonyericsson','sharp','amoi','panda','zte');
$pad_list=array('pad','gt-p1000');
$useragent=strtolower($_SERVER['HTTP_USER_AGENT']);
if(dstrpos($useragent,$pad_list)){
returnfalse;
}
if(($v=dstrpos($useragent,$mobilebrowser_list,true))){
$_G['mobile']=$v;
return'1';
}
if(($v=dstrpos($useragent,$touchbrowser_list,true))){
$_G['mobile']=$v;
return'2';
}
if(($v=dstrpos($useragent,$wmlbrowser_list))){
$_G['mobile']=$v;
return'3';//wml版
}
$brower=array('mozilla','chrome','safari','opera','m3gate','winwap','openwave','myop');
if(dstrpos($useragent,$brower))returnfalse;
$_G['mobile']='unknown';
//对于未知类型的浏览器,通过$_GET['mobile']参数来决定是否是手机浏览器
if(isset($_G['mobiletpl'][$_GET['mobile']])){
returntrue;
}else{
returnfalse;
}
}
/**
*判断$arr中元素字符串是否有出现在$string中
*@param$string$_SERVER['HTTP_USER_AGENT']
*@param$arr各中浏览器$_SERVER['HTTP_USER_AGENT']中必定会包含的字符串
*@param$returnvalue返回浏览器名称还是返回布尔值,true为返回浏览器名称,false为返回布尔值【默认】
*@authordiscuz3x
*@lastmodify2014-04-09
*/
functiondstrpos($string,$arr,$returnvalue=false){
if(empty($string))returnfalse;
foreach((array)$arras$v){
if(strpos($string,$v)!==false){
$return=$returnvalue?$v:true;
return$return;
}
}
returnfalse;
}
方法二:
<?php
functionisMobile()
{
//如果有HTTP_X_WAP_PROFILE则一定是移动设备
if(isset($_SERVER['HTTP_X_WAP_PROFILE']))
{
returntrue;
}
//如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
if(isset($_SERVER['HTTP_VIA']))
{
//找不到为flase,否则为true
returnstristr($_SERVER['HTTP_VIA'],"wap")?true:false;
}
//脑残法,判断手机发送的客户端标志,兼容性有待提高
if(isset($_SERVER['HTTP_USER_AGENT']))
{
$clientkeywords=array('nokia',
'sony',
'ericsson',
'mot',
'samsung',
'htc',
'sgh',
'lg',
'sharp',
'sie-',
'philips',
'panasonic',
'alcatel',
'lenovo',
'iphone',
'ipod',
'blackberry',
'meizu',
'android',
'netfront',
'symbian',
'ucweb',
'windowsce',
'palm',
'operamini',
'operamobi',
'openwave',
'nexusone',
'cldc',
'midp',
'wap',
'mobile'
);
//从HTTP_USER_AGENT中查找手机浏览器的关键字
if(preg_match("/(".implode('|',$clientkeywords).")/i",strtolower($_SERVER['HTTP_USER_AGENT'])))
{
returntrue;
}
}
//协议法,因为有可能不准确,放到最后判断
if(isset($_SERVER['HTTP_ACCEPT']))
{
//如果只支持wml并且不支持html那一定是移动设备
//如果支持wml和html但是wml在html之前则是移动设备
if((strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml')!==false)&&(strpos($_SERVER['HTTP_ACCEPT'],'text/html')===false||(strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml')<strpos($_SERVER['HTTP_ACCEPT'],'text/html'))))
{
returntrue;
}
}
returnfalse;
}
?>
判断手机端是IOS还是Android
代码如下:
/**
*判断是否手机访问
*/
functionis_mobile()
{
static$is_mobile;
if(isset($is_mobile))return$is_mobile;
if(empty($_SERVER['HTTP_USER_AGENT'])){
$is_mobile=false;
}elseif(strpos($_SERVER['HTTP_USER_AGENT'],'Mobile')!==false//manymobiledevices(alliPhone,iPad,etc.)
||strpos($_SERVER['HTTP_USER_AGENT'],'Android')!==false
||strpos($_SERVER['HTTP_USER_AGENT'],'Silk/')!==false
||strpos($_SERVER['HTTP_USER_AGENT'],'Kindle')!==false
||strpos($_SERVER['HTTP_USER_AGENT'],'BlackBerry')!==false
||strpos($_SERVER['HTTP_USER_AGENT'],'OperaMini')!==false
||strpos($_SERVER['HTTP_USER_AGENT'],'OperaMobi')!==false
){
$is_mobile=true;
}else{
$is_mobile=false;
}
return$is_mobile;
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!