内页Banner背景图
创络讲堂
白色波浪图形
灰色波浪图形 灰色波浪图形
白色波浪图形 白色波浪图形
白色波浪图形 白色波浪图形
白色波浪图形 白色波浪图形
本文于2014-12-04最后更新,距今已超过1年,如不符合现在的实际情况,可联系创络客服获取帮助或查阅近期文章。

手机版网站和电脑版网站的自动识别并跳转

大家可以尝试一下用手机访问我公司官方网站(www.chuangluo.com),系统会自动识别并跳转到手机版网站去;同样的,如果使用电脑访问手机版网站(m.chuangluo.com),系统也会自动识别并跳转到对应的电脑版网址去。目前已经有很多同时拥有手机版网站和电脑版网站的大型企业网站使用了这样的技术,那小型公司网站如果也有这种需求的话,应该如何实现呢?

实现手机版网站和电脑版网站的自动识别并跳转的条件

首先肯定是需要有两个不同版本的网站,这是必然的!第二个条件是这两个版本的网站数据要基本同步。以我公司官方网站为例,电脑版网站的页面比手机版的多,手机版有的页面,电脑版全部都有。这是因为我们电脑版网站上有很多静态的专题页,而手机版则没有,例如:网站价格评估、域名查询等。因此,我们只需要实现自动识别这一部分页面并跳转即可。

如何实现自动识别并跳转

由于我公司目前开发网站的语言是主流的PHP,因此我们此处只讨论PHP的实现方法,其他语言请直接参考本方法稍作修改即可。核心原理是通过程序判断$_SERVER[?HTTP_USER_AGENT?]中是否包含了手机访问的一些特征信息,例如屏幕尺寸,手机品牌或者其他特征信息。此处一提供两个方法供大家参考:

方法一:

判断函数

function is_mob() {
    $user_agent = $_SERVER[?HTTP_USER_AGENT?];
    $mob_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");
    $is_mob = false;
    foreach ($mob_agents as $device) {
     if (stristr($user_agent, $device)) {
   $is_mob = true;
   break;
     }
    }
    return $is_mob;
   }

调用函数

if(is_mob())
   {
  //通过$_SERVER["REQUEST_URI"]取出具体页面网址,修改域名后并跳转。
  }

方法二:

$ua = strtolower($_SERVER[?HTTP_USER_AGENT?]);
$uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|mobile)/i"
if(($ua == ?? || preg_match($uachar, $ua))&& !strpos(strtolower($_SERVER[?REQUEST_URI?]),?wap?))
{
 //通过$_SERVER["REQUEST_URI"]取出具体页面网址,修改域名后并跳转。
}

两种方法中推荐使用第一种,识别率相对较高;第二种效率更高,但识别率相对较低。两种方法均整理自互联网,大家在使用的时候可以稍加修改,以实现更多功能。

上一条 返回目录 下一条
更多阅读选择
相关文章
我们推荐了一些您可能也会感兴趣的文章
客服电话
  • 0755-23698839
  • 134-1757-3710
  • 132-6576-8769
创络建站客服二维码
不出售过期域名
微信咨询