我有这个代码,我试图将移动用户重定向到移动优化页面和桌面用户到桌面页面。
<?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 } ?>
有人可以帮我解决我的错误吗?
答案 0 :(得分:0)
试试这个。
<?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
}
?>
答案 1 :(得分:0)
<?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
}
?>
答案 2 :(得分:0)
@whyceewhite @Subin Thomas @rahul它实际上非常好用!我的服务器上出现语法错误,因为我在代码中忘记了一个大括号。这段代码没关系。我道歉!
I used <?php else { ?> instead of <?php } else { ?>