我正在使用此代码来查找用户是使用移动设备还是台式机,如果用户通过使用header
功能使用移动设备,则将用户重定向到移动文件夹,但这会导致“重定向错误过多”。
<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
$iPod = stripos($useragent, "iPod");
$iPad = stripos($useragent, "iPad");
$iPhone = stripos($useragent, "iPhone");
$Android = stripos($useragent, "Android");
$iOS = stripos($useragent, "iOS");
$Blackberry =stripos($useragent, "Blackberry");
$DEVICE = ($iPod||$iPad||$iPhone||$Android||$iOS||$Blackberry);
if (!$DEVICE) {
header('Location: http://localhost/pogester/view_post.php?post_id=495');
exit();
?>
<!-- What you want for all non-mobile devices. Anything with all HTML, PHP, CSS, even full page codes-->
<?php }else{
//echo $useragent;
header('Location: http://localhost/pogester/mobile/view_post.php?post_id=495');
exit();
}
?>
请注意,移动设备的路径不同
答案 0 :(得分:2)
您发布的这段代码是否存储在名为view_post.php的文件中?如果是这样,则此文件将无限期地重定向到其自身。
答案 1 :(得分:0)
每次此代码有效时,您都会获得重定向。为了避免这种情况,您不仅应该检查当前设备,还应该检查当前URL。像这样的东西:
if (!$DEVICE && strpos($_SERVER['REQUEST_URI'], "mobile/")) {
header('Location: http://localhost/pogester/view_post.php?post_id=495');
exit();
if ($DEVICE && !strpos($_SERVER['REQUEST_URI'], "mobile/")) {
header('Location: http://localhost/pogester/mobile/view_post.php?post_id=495');
exit();
}