根据访问请求客户端类型自动跳转到对应的页面地址,自动跳转到手机页面

在智能移动终端横行霸道的今天,使用移动终端来访问网站的用户是越来越多,但针对PC用户开发的网站,在移动终端上的体验非常差,这不,我们开始针对移动终端也制作了体验相对更好的页面,那么我们怎么才能知道用户使用的是哪种终端来访问我们的网站呢,总不能让用户再来记一遍我们的手机站域名吧,查阅资料,有很多方法可以实现这个需求,现在将发现的方法记录如下:

JS实现方法:

方法一:

 function mobile_device_detect(url)
 {
        var thisOS=navigator.platform;
        var os=new Array("iPhone","iPod","iPad","android","Nokia","SymbianOS","Symbian","Windows Phone","Phone","Linux armv71","MAUI","UNTRUSTED/1.0","Windows CE","BlackBerry","IEMobile");
 for(var i=0;i<os.length;i++)
        {
 if(thisOS.match(os[i]))
        {  
  window.location=url;
 }
          
 }
 //因为相当部分的手机系统不知道信息,这里是做临时性特殊辨认
 if(navigator.platform.indexOf('iPad') != -1)
        {
  window.location=url;
 }
 //做这一部分是因为Android手机的内核也是Linux
 //但是navigator.platform显示信息不尽相同情况繁多,因此从浏览器下手,即用navigator.appVersion信息做判断
  var check = navigator.appVersion;
  if( check.match(/linux/i) )
          {
   //X11是UC浏览器的平台 ,如果有其他特殊浏览器也可以附加上条件
   if(check.match(/mobile/i) || check.match(/X11/i))
                 {
   window.location=url;
   } 
 }
 //类in_array函数
 Array.prototype.in_array = function(e)
 {
  for(i=0;i<this.length;i++)
  {
   if(this[i] == e)
   return true;
  }
  return false;
 }
 }
mobile_device_detect("手机站地址");

方法二:

try {
var urlhash = window.location.hash;
if (!urlhash.match("fromapp"))
{
if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i)))
{
window.location="http://m.16css.com/";
}
}
}
catch(err)
{
}

方法三:

// JavaScript Document
function urlredirect() {
    var sUserAgent = navigator.userAgent.toLowerCase(); 
    if ((sUserAgent.match(/(ipod|iphone os|midp|ucweb|android|windows ce|windows mobile)/i))) {
        // PC跳转移动端
        var thisUrl = window.location.href;
        window.location.href = thisUrl.substr(0,thisUrl.lastIndexOf('/')+1)+'mobile/';
         
    }
}
urlredirect();

php实现方法:

<?php
$agent = $_SERVER['HTTP_USER_AGENT'];
if(
    strpos($agent,"comFront")
    || strpos($agent,"iPhone")//iPhone
    || strpos($agent,"MIDP")//JAVA
    || strpos($agent,"Opera Mini")//Opera for mobile
    || strpos($agent,"UCWEB")//UC Mobile Limited
    || strpos($agent,"Android")//android
    || strpos($agent,"Windows CE")//Win CE
    || strpos($agent,"Windows mobile")//Win phone
    || strpos($agent,"SymbianOS"))//Symbian
    {
    header("Location:手机站地址");
}else {
    header("Location:PC站地址");
    }
?>

 

posted @ 2016-11-21 09:59  Yiven  阅读(4019)  评论(0编辑  收藏  举报