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

创络讲堂 2014-12-04 08:00:00 6353次 https://www.chuangluo.com/news_415.html 作者:创络科技

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

大家可以尝试一下用手机访问我公司官方网站(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"]取出具体页面网址,修改域名后并跳转。
}

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

  • 综合评述网站权重的来源
    2012-03-13 08:00:00
    网站的权重高低对网站的排名优劣会产生直接的影响,通常关键词相同的情况下,权重高的网站排名会比权重低的网站好很多。为了让创络的所有客户对网站本身的排名有一个清楚的认识, 深圳网站建...
  • 如何设计硬气一点的网站
    2012-07-19 08:00:00
    一般客户需要硬气一点的网站的情况很少,毕竟都谁希望自己的 网站设计 出来后可以让访问者感到亲切、柔和。然而,事情并没有绝对,深圳网站建设公司【创络】有时候也会遇到需要将自己的网站...
  • 在后台怎样设置才能屏蔽网站右键禁止复制
    2013-05-16 08:00:00
    以前因为没有做过网站右键屏蔽功能,所以我们自己的网站有吃过亏,辛辛苦苦编辑的原创文章,被其他网络公司随便就复制走了。 为了避免大家也遇到这样的事情,我们专门在后台开发了一个新功...
标准建站流程
客服电话
  • 0755-23698839