奇怪的 PHP 重定向行为 - 所有 if 语句工作正常,而 else 语句覆盖并成为默认重定向


Strange PHP redirect behavior - All if statements work fine, while the else statement overrides and becomes the default redirect

我有一个简短的问题,对这里的大多数人来说应该很简单。在一个网站上,我通过 1 个称为访问的简单 PHP 重定向重定向我的所有链接.php

我正在像这样格式化我的出站重定向..

visit.php?url=google.com
visit.php?url=yahoo.com
visit.php?url=aol.com

然后我的访问.php看起来像这样...

<?php
$url = htmlspecialchars($_GET['url']);
if($url == 'google.com'){
header("Location: http://google.com");
}
if($url == 'yahoo.com'){
header("Location: http://yahoo.com");
}
if($url == 'aol.com'){
header("Location: http://aol.com");
}
else {
header("Location: http://mydomain.com/404");
}

我遇到的问题是 else 语句。当我从脚本中删除它时,一切都按预期工作。当我把它放进去时(所以任何拼写错误都会重定向到 404),它会覆盖我在脚本中定义的所有链接并重定向到 404。

我遇到的另一个问题是不使用 ?url= 变量。如果我网站上的用户将链接复制到他们的剪贴板并将其更改为除 ?url= 以外的任何内容,我想重定向到 404。我尝试用于完成此操作的代码如下所示...

if(!isset($_GET['url'])){
header("Location: http://mydomain.com/404");
}

我的逻辑是...如果未设置 ?url= 变量,只需重定向到 404,因为一定有人在玩。如果有人加载 visit.php?id=blah,它应该重定向到 404。

但是,我没有得到预期的行为。我对PHP和编程很陌生...所以如果这些问题是幼儿园水平,请原谅我。谢谢。

您应该

使用 if/else if/else 构造,否则 else 语句仅附加到最后一个if

当最后一个if的条件不满足时,它自然会执行else子句。

您的初始方法(已更正):

<?php
$url = htmlspecialchars($_GET['url']);
if($url == 'google.com'){
    header("Location: http://google.com");
}
else if($url == 'yahoo.com'){
    header("Location: http://yahoo.com");
}
else if($url == 'aol.com'){
    header("Location: http://aol.com");
}
else {
    header("Location: http://mydomain.com/404");
}

常用方法:

    <?php
$url = htmlspecialchars($_GET['url']);
if($url == 'google.com'){
    header("Location: http://google.com");
    exit;
}
if($url == 'yahoo.com'){
    header("Location: http://yahoo.com");
    exit;
}
if($url == 'aol.com'){
    header("Location: http://aol.com");
    exit;
}
header("Location: http://mydomain.com/404");
exit;

最佳(推荐)方法:

switch($url) {
    case 'google.com':
        header("Location: http://google.com");
        break;
    case 'yahoo.com':
        header("Location: http://yahoo.com");
        break;
    case 'aol.com':
        header("Location: http://aol.com");
        break;
    default:
        header("Location: http://mydomain.com/404");
        break;
}

嗯......更好:

$list_of_places = array(
    'google.com',
    'yahoo.com',
    'aol.com'
);
if(in_array($url, $list_of_places)) {
    header("Location: http://".$url);
    exit;
}
else {
    header("Location: http://mydomain.com/404");
    exit;
}

发出 header('Location: ...') 后代码继续执行。后续标头将覆盖先前设置的标头。如果您不想执行任何进一步的操作,则需要停止代码执行

header(...);
exit;

此外,else只适用于最后的if,因为语句没有链接在一起。

应该是

<?php
$url = htmlspecialchars($_GET['url']);
if($url == 'google.com'){
header("Location: http://google.com");
}
else if($url == 'yahoo.com'){
header("Location: http://yahoo.com");
}
else if($url == 'aol.com'){
header("Location: http://aol.com");
}
else {
header("Location: http://mydomain.com/404");
}
?>

作为一个懒惰的程序员,我重新命令重写你的代码

   $url = htmlspecialchars(!empty($_GET['url'])?$_GET['url']:"");
    switch($url)
    {
      case 'google.com': 
         header("Location: http://google.com");
         break;
      case 'yahoo.com': 
         header("Location: http://yahoo.com");
         break;
      case 'aol.com': 
         header("Location: http://aol.com");
         break;
      default:
         header("Location: http://mydomain.com/404");
    }

如果只是if,请使用if else代替(因为你正在执行最后一个"else"语句):

<?php
    $url = htmlspecialchars($_GET['url']);
    if($url == 'google.com'){
        header("Location: http://google.com");
    } else if ($url == 'yahoo.com') {
        header("Location: http://yahoo.com");
    } else if ($url == 'aol.com'){
        header("Location: http://aol.com");
    } else {
        header("Location: http://mydomain.com/404");
    }
?>

或者使用 switch 语句:

<?php
    $url = htmlspecialchars($_GET['url']);
    switch ($url) {
        case 'google.com':
            header("Location: http://google.com");
            break;
        case 'yahoo.com':
            header("Location: http://yahoo.com");
            break;
        case 'aol.com':
            header("Location: http://aol.com");
            break;
        default:
            header("Location: http://mydomain.com/404");
    }
?>

使用这两种解决方案中的任何一种,如果您的$url不等于您希望它等于的,则用户将被定向到您的 404 页面。

好的,我想出了"如果尝试不同的键,则重定向到 404"问题(只需在第一个声明中添加错误控制运算符)。

我的代码现在看起来像这样...

<?php
$url = @htmlspecialchars($_GET['url']);
if(!isset($_GET['url'])){
header("Location: http://mydomain.com/404");
exit;
}
elseif($url == 'google.com'){
header("Location: http://google.com");
exit;
}
elseif($url == 'yahoo.com'){
header("Location: http://yahoo.com");
exit;
}
elseif($url == 'aol.com'){
header("Location: http://aol.com");
exit;
}
else{
header("Location: http://mydomain.com/404");
exit;
}