随着当今移动设备的普及,上网已经比过去更加方便。针对Android智能手机,iPhone/iPad等移动终端,很多网站都相继推出了针对电脑和这类手机等移动设备访问的网页。本文所述的实例代码就可以实现根据这些移动设备终端而自动跳转到适合它们浏览的页面。即判断PC端或手机站智能端并跳转。
<?php
/**
* 客户端相关操作辅助类
*
* @author bob <983194768@qq.com>
*/
class HelpClient
{
/**
* 判断访问是否来自移动端
*
*/
static public 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 = self::checkSubstrs($mobile_os_list,$useragent_commentsblock) ||
self::checkSubstrs($mobile_token_list, $useragent);
if ($found_mobile) {
return true;
} else {
return false;
}
}
static private function checkSubstrs($substrs,$text)
{
foreach($substrs as $substr)
if(false!==strpos($text,$substr))
{
return true;
}
return false;
}
}