[英]PHP Mobile redirect doesn't work
I have this code where I am trying to redirect mobile users to mobile optimized page and desktop users to desktop page. 我在尝试将移动用户重定向到移动优化页面并将桌面用户重定向到桌面页面的这段代码中。
<?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 } ?>
Now I am trying to further redirect mobile Android and Iphone users by doing elseif statement, but I am getting error. 现在,我试图通过执行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 } ?>
Could someone help me where I am doing mistake? 有人可以在我做错地方帮助我吗?
Try this. 尝试这个。
<?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
}
?>
<?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
}
?>
@whyceewhite @Subin Thomas @rahul It actually works perfectly fine! @whyceewhite @Subin Thomas @rahul实际上效果很好! I was getting syntax error on my server, because I forgot one curly bracket in my code.
我在服务器上遇到语法错误,因为我在代码中忘记了一个花括号。 This code here is OK.
这段代码可以。 I apologize!
我道歉!
I used <?php else { ?> instead of <?php } else { ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.