【问题标题】:PHP Mobile redirect doesn't workPHP 移动重定向不起作用
【发布时间】:2015-08-27 17:41:40
【问题描述】:

我有这段代码,我试图将移动用户重定向到移动优化页面,并将桌面用户重定向到桌面页面。

<?php if(stristr($_SERVER['HTTP_USER_AGENT'],'iPad')||
         stristr($_SERVER['HTTP_USER_AGENT'],'iPhone')||
         stristr($_SERVER['HTTP_USER_AGENT'],'iPod')||
         stristr($_SERVER['HTTP_USER_AGENT'],'Android')){ ?>

  mobile-page.com

<?php } else { ?>
 dekstop-page.com

<?php } ?>

现在我正在尝试通过执行 elseif 语句来进一步重定向移动 Android 和 Iphone 用户,但我收到了错误。

 <?php if(stristr($_SERVER['HTTP_USER_AGENT'],'iPad')||
         stristr($_SERVER['HTTP_USER_AGENT'],'iPhone')||
         stristr($_SERVER['HTTP_USER_AGENT'],'iPod')){ ?>

  iphone-page.com

 <?php } elseif(stristr($_SERVER['HTTP_USER_AGENT'],'Android')){ ?>

  android-page.com

<?php } else { ?>
 dekstop-page.com

<?php } ?>

有人可以帮我解决我做错的地方吗?

【问题讨论】:

  • 如果需要自动重定向,请使用标头。
  • 能否提供您收到的错误信息,以便我们更好地确定您遇到的问题?

标签: php android redirect mobile


【解决方案1】:

试试这个。

<?php
  if(stristr($_SERVER['HTTP_USER_AGENT'],'iPad')||
             stristr($_SERVER['HTTP_USER_AGENT'],'iPhone')||
             stristr($_SERVER['HTTP_USER_AGENT'],'iPod')||
             stristr($_SERVER['HTTP_USER_AGENT'],'Android'))
             { 
             header( 'Location: mobilepage.html') ; //forward to mobile page
        } else {
        header( 'Location: desktoppage.html') ;  //forward to desktop page
       }
?>

【讨论】:

  • 但我想将 Andorid 用户重定向到 andoridpage.com,将 Iphone 用户重定向到 iphonepage.com,将 PC 用户重定向到 dekstoppage.com
  • 更改页面名称老兄.. 例如:'Location: blablabla.html'
【解决方案2】:
<?php
$UserInfo = $_SERVER['HTTP_USER_AGENT'];
if (stristr($UserInfo, 'iPad') || stristr($UserInfo, 'iPhone') || stristr($UserInfo, 'iPod')) {
    header('Location: iphone-page.com'); //directs to iphone mobile page                     
} elseif (stristr($UserInfo, 'Android')) {
    header('Location: android-page.com'); //directs to mobile page  
} else {
    header('Location: dekstop-page.com'); //directs to dekstop page
}
?>

【讨论】:

    【解决方案3】:

    @whyceewhite @Subin Thomas @rahul 它实际上工作得很好!我的服务器出现语法错误,因为我在代码中忘记了一个大括号。这里的代码是可以的。我道歉!

    I used <?php  else { ?> instead of <?php } else { ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 2019-01-25
      相关资源
      最近更新 更多