PHP 标头位置重定向不起作用 - 为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2710079/
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 07:26:20  来源:igfitidea点击:

PHP header location-redirect doesn't work - why?

phpredirectheaderlocation

提问by Industrial

Here's my file. I want to make it redirect, but nothing happens. To check out what is going on, I added an echo before the header part.

这是我的文件。我想让它重定向,但没有任何反应。为了检查发生了什么,我在标题部分之前添加了一个 echo。

It neither throws an error or redirect to index.php. What is wrong? I have turned output buffering on/off, but nothing makes it redirect. What can I do?

它既不会抛出错误也不会重定向到 index.php。怎么了?我已经打开/关闭输出缓冲,但没有任何东西使它重定向。我能做什么?

<?
error_reporting(E_ALL);
echo 'This is an error';

header("Location: login.php");
die();
?>

Thanks

谢谢

采纳答案by Industrial

I reminded myself that I had xDebug installed on the actual test environment and after googling it, I found this site: http://bugs.xdebug.org/view.php?id=532

我提醒自己,我在实际测试环境中安装了 xDebug,在谷歌搜索之后,我找到了这个站点:http://bugs.xdebug.org/view.php?id=532

So I'll downloaded the last version of xDebug and changed the php.ini accordingly for the new file and everything works out like a charm. Headers are being sent - the redirecetion is done and errors are displayed.

因此,我将下载 xDebug 的最新版本,并相应地为新文件更改了 php.ini,一切都变得很完美。正在发送标头 - 重定向已完成并显示错误。

Thanks everybody for your help!

感谢大家的帮助!

回答by Soufiane Hassou

From PHP documentation:

来自PHP 文档

header()must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

header()必须在发送任何实际输出之前调用,无论是通过普通的 HTML 标签、文件中的空行还是来自 PHP。

And in your case, you are using echobefore header()

在您的情况下,您echo之前使用的是header()

回答by jah

Do you have short tags enabled? try it with the long tag <?php:

您是否启用了短标签?用长标签试试<?php

<?php
error_reporting(E_ALL);
header("Location: login.php");
die();
?>

回答by Your Common Sense

Is display_errors enabled?
@Gumbo - It sure is!

是否启用了 display_errors?
@Gumbo - 确实如此!

well, it sure is not. because

好吧,它肯定不是。因为

To check out what is going on, I added an echo before the header part.

为了检查发生了什么,我在标题部分之前添加了一个 echo。

if you'd have display_errors enabled, it would display an error, at least when you turned output buffering on.

如果您启用了 display_errors,它会显示一个错误,至少当您turned output buffering on.

So, first of all make sure you can see error messages.
just print out an undefined variable,

因此,首先确保您可以看到错误消息。
只需打印出一个未定义的变量,

error_reporting(E_ALL);
echo $undef9306;

I am sure you won't see it. So, you have to turn displaying errors on

我相信你不会看到的。所以,你必须打开显示错误

Next, on the server side you can use headers_sent()function to see if headers were sent. On client side use HTTP sniffer to see if anything were sent

接下来,在服务器端,您可以使用headers_sent()函数来查看是否发送了标头。在客户端使用 HTTP 嗅探器查看是否发送了任何内容

And check your file for the BOM.

并检查您的 BOM 文件。