我的移动版PHP重定向无效

时间:2017-07-26 16:18:24

标签: php android iphone

帮助!我尝试了一些不同的代码,但没有任何方法可以重定向到移动设备。我在godaddy服务器上,这是我的代码,在index.php

<?php
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");

if ($iphone || $android || $palmpre || $ipod || $berry == true) 
{ 
header('Location: http://mobile.practicallunacy.com/');
}
?>

--- ---编辑 这就是我改变它的原因。试着对我轻松,我对php不太了解。

<?php
    $iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    $android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
    $palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
    $berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
    $ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");

if ($iphone = true) if ($android = true) if ($palmpre = true) if ($ipod =     true) if ($berry = true) 
{ 
header('Location: http://mobile.practicallunacy.com/');
}
?>

---- ----编辑

基于其他人告诉我要做的事情,这是我提出的新代码......它仍然无效。

<?php
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");

if ($iphone == true) 
{ 
header('Location: http://mobile.practicallunacy.com/');
}
if ($android == true)
{ 
header('Location: http://mobile.practicallunacy.com/');
}
if ($palmpre == true)
{ 
header('Location: http://mobile.practicallunacy.com/');
}
if ($ipod == true)
{ 
header('Location: http://mobile.practicallunacy.com/');
}
if ($berry == true) 
{ 
header('Location: http://mobile.practicallunacy.com/');
}
?>

2 个答案:

答案 0 :(得分:0)

if(($iphone || $android || $palmpre || $ipod || $berry) === true){ 
header('Location: http://mobile.practicallunacy.com/');
}

你需要在if语句中使用双重语句,请注意the operators

之间的差异

答案 1 :(得分:0)

你的语法都错了,这段代码不会运行。你的最后一次尝试在语法上是正确的,但没有说明strpos()的变幻莫测,如果匹配,可以返回0

<?php
$iphone  = strpos($_SERVER['HTTP_USER_AGENT'], "iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'], "Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'], "webOS");
$berry   = strpos($_SERVER['HTTP_USER_AGENT'], "BlackBerry");
$ipod    = strpos($_SERVER['HTTP_USER_AGENT'], "iPod");

if ($iphone !== false || $android !== false || $palmpre !== false || $ipod !== false || $berry !== false) {
    header("Location: http://mobile.practicallunacy.com/");
}

注意使用严格的相等检查; 0 == true0 !== true

尽管如此,这对正则表达式搜索很有用:

<?php
if (preg_match("/iphone|android|webos|blackberry|ipod/i", $_SERVER['HTTP_USER_AGENT'])) {
    header("Location: http://mobile.practicallunacy.com/");
}