您的位置 首页 php

移动互联网时代PHP如何帮你判断网站用户是电脑还是手机呢?

现如今已经是移动互联网时代了,如果你想统计一下个人网站或者公司网站PC端和手机移动端的流量情况,或者你需要为移动端做一些方案的时候,那么我们就需要对访问网站的用户做一下判断了。好了,闲话不扯了,下面看下PHP如何判断用户是电脑访问还是移动端访问的吧

<?php

function isMobile(){

$useragent=isset($_SERVER[‘HTTP_USER_AGENT’]) ? $_SERVER[‘HTTP_USER_AGENT’] : ”;

$useragent_commentsblock=preg_match(‘|\(.*?\)|’,$useragent,$matches)>0?$matches[0]:”;

$ Mobile _os_list=array(‘Google Wireless Transcoder’,’Windows CE’,’ WindowsCE ‘,’Symbian’,’Android’,’armv6l’,’armv5′,’Mobile’,’ CentOS ‘,’mowser’,’AvantGo’,’Opera Mobi’,’J2ME/MIDP’,’Smartphone’,’Go.Web’,’Palm’,’iPAQ’);

$mobile_token_list=array(‘Profile/MIDP’,’Configuration/CLDC-‘,’160×160′,’176×220′,’240×240′,’240×320′,’320×240′,’UP.Browser’,’UP.Link’,’SymbianOS’,’PalmOS’,’PocketPC’,’SonyEricsson’,’ Nokia ‘,’BlackBerry’,’Vodafone’,’BenQ’,’Novarra-Vision’,’Iris’,’NetFront’,’HTC_’,’Xda_’,’SAMSUNG-SGH’,’Wapaka’,’DoCoMo’,’iPhone’,’iPod’);

$found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock)||CheckSubstrs($mobile_token_list,$useragent);

if($found_mobile){

return true;

}else{

return false ;

}

}

function CheckSubstrs($substrs,$text){

foreach ($substrs as $substr){

if(false!==strpos($text,$substr)){

return true;

}

return false;

}

}

if(isMobile()){

echo ‘手机登录 m.5itjob.com’;

}else{

echo ‘电脑登录 www.5itjob.com’;

}

文章来源:智云一二三科技

文章标题:移动互联网时代PHP如何帮你判断网站用户是电脑还是手机呢?

文章地址:https://www.zhihuclub.com/154064.shtml

关于作者: 智云科技

热门文章

网站地图