Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
奇怪的PHP重定向行为-所有if语句都可以正常工作,而else语句将覆盖并成为默认重定向_Php_Redirect_Http Status Code 301_If Statement - Fatal编程技术网

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

奇怪的PHP重定向行为-所有if语句都可以正常工作,而else语句将覆盖并成为默认重定向,php,redirect,http-status-code-301,if-statement,Php,Redirect,Http Status Code 301,If Statement,我有一个简单的问题,对这里的大多数人来说应该很简单。在一个网站上,我通过一个名为visit.PHP的简单PHP重定向重定向我的所有链接 我正在格式化我的出站重定向,如下 visit.php?url=google.com visit.php?url=yahoo.com visit.php?url=aol.com 然后我的visit.php看起来像这样 <?php $url = htmlspecialchars($_GET['url']); if($url == 'google.com')

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

我正在格式化我的出站重定向,如下

visit.php?url=google.com
visit.php?url=yahoo.com
visit.php?url=aol.com
然后我的visit.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");
}
if(!isset($_GET['url'])){
header("Location: http://mydomain.com/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;
}
我的逻辑是。。。如果没有设置?url=变量,只需重定向到404,因为一定有人在玩。如果有人加载visit.php?id=blah,它应该重定向到404


然而,我没有得到预期的行为。一般来说,我对PHP和编程都很陌生。。。如果这些问题是幼儿园级别的,请原谅我。谢谢。

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

当最后一个
的条件未满足时,它将自然执行
else
子句

您的初始方法(已更正): 六羟甲基三聚氰胺六甲醚。。。更好的是:
发出
标题('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”语句):


或者使用switch语句:

<?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']);
    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($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");
}
if(!isset($_GET['url'])){
header("Location: http://mydomain.com/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;
}

如果$url=google,则执行位置goodle,因为这是有效的,不执行位置yahoo,因为无效,不执行位置aol,因为无效;但因为最后一个是无效的,它确实执行了ELSETH,这太棒了,谢谢!我遇到的最后一个问题是,如果有人决定将visit.php?url=google.com更改为另一个键,比如visit.php?haha=blah。。。我的代码现在的编写方式没有重定向到404。。。我遇到了一个严重的PHP错误。我的if(!isset($\u GET['url'])代码没有像我预期的那样工作。我编写的所有示例即使在
$\u GET['url']==NULL的情况下也能工作(这是未定义的情况)。
<?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;
}