一聚教程网:一个值得你收藏的教程网站

热门教程

PHP验证终端类型是否为手机的简单实例

时间:2022-06-24 22:42:33 编辑:袖梨 来源:一聚教程网

 代码如下 复制代码

/**

 * 判断当前访问的用户是 PC端 还是 手机端 返回true 为手机端 false 为PC 端

 * @return boolean

 */

functionisMobile(){

  $useragent=isset($_SERVER['HTTP_USER_AGENT']) ?$_SERVER['HTTP_USER_AGENT'] :'';

  //echo $useragent;die;

  $useragent_commentsblock=preg_match('|(.*?)|',$useragent,$matches)>0?$matches[0]:'';   

  functionCheckSubstrs($substrs,$text){

    foreach($substrsas$substr)

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

        returntrue;

      }

      returnfalse;

  }

  $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){

    returntrue;

  }else{

    returnfalse;

  }

}

热门栏目