PHP - 函数内的 header("Location:") 重定向而不调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2114039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:05:10  来源:igfitidea点击:

PHP - header("Location:") inside a function redirects without calling function

phpredirectheader

提问by Gal

I'm using a function called headerLocation()in order to redirect properly. This is the pertinent part of the function, I'm using it to redirect and then display an appropriate message (personal preference).

我正在使用一个调用函数headerLocation()来正确重定向。这是函数的相关部分,我用它来重定向,然后显示适当的消息(个人偏好)。

function headerLocation($location,$message)
{
    $_SESSION['output'] = $message;
    header("Location: ". $location);
}

The problem is, when I run the script, the header("Location: ". $location);part is activated without even calling the function (when I initially require it for php parsing). This renders the function useless.

问题是,当我运行脚本时,该header("Location: ". $location);部分甚至没有调用函数就被激活(当我最初需要它进行 php 解析时)。这使得函数无用。

Is there any way around this?

有没有办法解决?

采纳答案by Bobby Hyman

That should not happen. Either you arecalling the function somewhere, or another part of the code is writing out the header. Can you add some debug to that function to check?

那不应该发生。要么你调用函数的地方,或代码的另一部分是写出来的头。您可以向该功能添加一些调试以进行检查吗?

回答by danielrsmith

In addition to the feedback already given. It is a good idea to exitafter you redirect.

除了已经给出的反馈。exit在您重定向之后,这是一个好主意。

function headerLocation($location,$message)
{
    $_SESSION['output'] = $message;
    header("Location: ". $location);
    exit;
}

回答by davidosomething

Just a guess, perhaps you should buffer your output ( ob_start() ) This will cause headers to be sent only when the output is flushed, allowing the rest of your code to execute.

只是猜测,也许您应该缓冲输出( ob_start() )这将导致仅在刷新输出时发送标头,从而允许执行其余代码。

回答by Alix Axel

It makes no sense for the header()function to redirect without calling headerLocation()first.

header()函数在没有headerLocation()先调用的情况下进行重定向是没有意义的。

My guess is that you're not seeing $_SESSION['output']hold $message, and that makes you think the function is not being executed. Try writing to a new file instead, does that work? I bet it will.

我的猜测是您没有看到$_SESSION['output']hold $message,这让您认为该函数没有被执行。尝试写入一个新文件,这行得通吗?我打赌它会的。

The reason $_SESSIONmight not be holding your values is probably because of P3Pand your browser and / or PHP configuration.

原因$_SESSION可能没有保存您的值可能是因为P3P和您的浏览器和/或 PHP 配置。

Also, are you sure you don't wanna call die()/ exit()after the header()redirect?

另外,你确定你不想在重定向后调用die()/吗?exit()header()

回答by Joel Etherton

The redirect happens too fast for the $_Session to be written properly. Use

重定向发生得太快,无法正确写入 $_Session。用

session_write_close();

before the header call.

在标题调用之前。

Edit: Removed the ridiculous $ in front of the function call.

编辑:删除了函数调用前面的荒谬 $ 。