Php 如何将移动用户重定向到移动页面

Php 如何将移动用户重定向到移动页面,php,mobile,Php,Mobile,我为现有网页创建了移动站点,并将代码放在移动文件夹中 就像www.testing.com/mobile一样,它工作得很好 但我需要做的是,如果从手机访问的用户应该重定向到手机页面,从网络访问的用户将转到默认页面 在这里,我使用: <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0" /> 为了适应页面,但url显示如移动设备的m.tesing.com如何

我为现有网页创建了移动站点,并将代码放在移动文件夹中 就像www.testing.com/mobile一样,它工作得很好

但我需要做的是,如果从手机访问的用户应该重定向到手机页面,从网络访问的用户将转到默认页面

在这里,我使用:

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0" /> 

为了适应页面,但url显示如移动设备的m.tesing.com如何做到这一点


通过浏览器代理或任何其他方式。谢谢。

不用jquery,您可以使用简单的javascript来检测它:

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
 window.location.replace("http://m.tesing.com");
 //or window.location = "http://m.tesing.com";
}
或者使用CSS的其他技术

/* Smartphones ----------- */
@media only screen and (max-width: 760px) {
  #some-element { display: none; }
}
jQuery:

$( document ).ready(function() {        
    if( $('#some-element').css('display')=='none' {
        window.location.replace("http://m.tesing.com");
     //or window.location = "http://m.tesing.com";    
    }
 });
下载此文件并在头标签中进行更改

 <head>
                <script src="detectmobile.js"></script>
                <script>
                        try {
                                // This is the URL where mobile
                                detectmobile.defaultMobileURL = "http://m.testing.com";
                                detectmobile.process();
                        } catch(e) {
                                // Make sure that in the fault modes
                                // we do not interrupt execution of other Javascript code
                        }
                </script>
        </head>

试一试{
//这是移动设备所在的URL
detectmobile.defaultMobileURL=”http://m.testing.com";
detectmobile.process();
}捕获(e){
//确保在故障模式下
//我们不会中断其他Javascript代码的执行
}

使用此代码检测移动设备,然后重定向它

    <script language="javascript">
      if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) 
      {
        window.location = "http://m.domain.com";
      }
    </script>

if(/Android | webOS | iPhone | iPad | iPod | BlackBerry/i.test(navigator.userAgent))
{
window.location=”http://m.domain.com";
}
test()方法已用于测试字符串中的匹配项,如果找到匹配项,则会发生重定向。在main index.php页面顶部添加此代码

在mobile index.php上使用此代码重定向回

    <script language="javascript">
      if( !/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) 
      {
        window.location = "http://testing.com";
      }
    </script>

if(!/Android | webOS | iPhone | iPad | iPod | BlackBerry/i.test(navigator.userAgent))
{
window.location=”http://testing.com";
}

至于m.testing.com,你必须创建一个子域名。

检查答案。有很多方法,没有一种方法是最好的。谷歌是你的朋友:谢谢你的回复