PHP header() 不起作用

发布于 2024-12-01 00:10:26 字数 227 浏览 2 评论 0原文

有人知道为什么我的 header() 不重定向吗?

我的脚本的最后一部分是:

  header("location: test.php");
  die('died');

它写道:

died.

:(((

它应该在死之前重定向,但它没有。

你有什么想法吗?

Does somebody know why my header() does not redirect?

The last part of my script is:

  header("location: test.php");
  die('died');

It writes:

died.

:(((

It should has to redirect before it dies, but it does not.

Do you have you any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

川水往事 2024-12-08 00:10:26

您可能在向浏览器输出一些文本/HTML 之后调用 header(),这是禁忌。即使在调用之前发送了一个输出空间,您的 header() 调用也将被忽略。

换句话说,在向用户显示任何内容之前,您的 header() 代码需要位于脚本的开头。如果仍然不起作用,请确保您的 php 标记之外没有任何空格或其他空白。

It's probably that you're calling header() after you are outputting some text/HTML to the browser, which is a no-no. Your header() call will be ignored if even so much as a single space of output has been sent before the call.

In other words, your header() code needs to be at the start of your script, before you display anything to the user. If that still isn't working, make sure you don't have any spaces or other whitespace by mistake outside of your php tags.

泪之魂 2024-12-08 00:10:26

您应该为位置标头使用完全限定的地址,并且输出任何内容:

header('Location: http://example.com/test.php');
die();

此外,请确保在设置标头之前没有发送其他内容,否则将不会发送,因为标头将具有已经发送到浏览器了。

You should use a fully-qualified address for your location header, and not output any content:

header('Location: http://example.com/test.php');
die();

Additionally make sure that no other content was sent before setting the header, otherwise it wont be sent as the headers will have already been sent to the browser.

暮光沉寂 2024-12-08 00:10:26

也许在 header 之前有一些不可见的输出,这会阻止设置 header,并且信息警告会被抑制。

另外,位置标头应包含绝对 URL:

// Show Errors
error_reporting(E_ALL);
ini_set('display_errors','On');
// Redirect
header('Location: http://example.com/path/test.php');
die('redirect');

Maybe there is some invisible output before header, which is preventing setting header, and informative warnings are suppressed.

Additionally Location-Headers should contain an absolute URL:

// Show Errors
error_reporting(E_ALL);
ini_set('display_errors','On');
// Redirect
header('Location: http://example.com/path/test.php');
die('redirect');
不语却知心 2024-12-08 00:10:26

位置: 应为位置:

此外...

  • 滚动到本页顶部。
  • 单击您的帐户名。
  • 单击随机问题 选中
  • 随机答案
  • 返回以查找更多严肃的答案

祝你好运。

location: should be Location:.

Moreover...

  • Scroll to the top of this page.
  • Click on your account name.
  • Click on a random question
  • Check mark a random answer
  • Come back to find more serieus answers

Good luck.

注定孤独终老 2024-12-08 00:10:26

只需删除 php 标记之前的所有内容即可解决此问题。

删除 php 标记之前的空格

选择标记之前的所有内容,然后按擦除按钮。

Just resolve it by removing ALL things before the php tag.

Remove the SPACE before the php tag

Select all before the tag and then push the erase button.

不再见 2024-12-08 00:10:26

我也被这个错误困扰,并尝试使用 error_reporting(E_ALL) 显示错误,
但这对我不起作用。信息:我在 Strato 上使用托管服务。

Floem 回答了所有这些问题的解决方案,感谢这个出色的设置。

如果您输入 error_reporting(E_ALL) 一次后没有收到错误,
-> 您必须另外添加 ini_set('display_errors', 'On'),以强制 PHP 显示所有错误。这解决了我的错误问题,

然后可以读出为什么 PHP 不会进行重定向。
我这边的一个提示:我建议您构建一个小重定向类,如以下视频所示: 重定向类以方便使用,如果您要使用此重定向超过通常。

I was stuck on this error, too, and tried to show errors with error_reporting(E_ALL),
but this didn't worked for me. Info: I was using a hosting service on Strato.

The solution to all this was answered by Floem, thanks for this great setup.

If you ever don't get errors in case you put error_reporting(E_ALL) once in,
-> You must additionally add ini_set('display_errors', 'On'), to force PHP to show all your Errors. This solved my Error issue on

Then it's possible to read out, why PHP wouldn't make your redirect.
One tip on my side: I would recommend you to build a little redirect Class, as shown in this Video: Redirect Class for easy use, if you're going to use this redirect more than usual.

木落 2024-12-08 00:10:26

如果 HTML 文件中直接/间接需要包含 'header()' 指令的文件,则指令 {include 'someFile'} 应该是该 HTML 文件的第一行。将此说明放在 HTML 文件的顶部,如此图片所示。

If the file containing the 'header()' instruction is required directly/indirectly in a HTML file, then the instruction {include 'someFile'} should be the first line of that HTML file. Place this instruction on the top of your HTML file, as shown in this image.

有深☉意 2024-12-08 00:10:26

添加

ob_start();

之前

header("location: test.php");

Add

ob_start();

before

header("location: test.php");
就是爱搞怪 2024-12-08 00:10:26

在最初的问题中,die() 函数在之前有一个 header() 的情况下被执行。
我遇到了类似的问题,所以我创建了下面的代码来测试。

header("Location:index.php");

$file = fopen("test.txt","a");
fwrite($file,date("Y-m-d H:i:s")."\n");
fclose($file);

然后...?那么,执行 header() 函数之后以及只有 header() 返回到 index.php 页面之后出现的代码。

所有这些都表明 header() 函数可以在脚本末尾使用来引用另一个脚本。
但是要小心如果它位于脚本的开头,它不会阻止脚本运行。

In the original question the die() function is executed while there is a header() just before.
I had a similar problem so I created the code below to test.

header("Location:index.php");

$file = fopen("test.txt","a");
fwrite($file,date("Y-m-d H:i:s")."\n");
fclose($file);

And...? Well the code present after the header() function is executed and after only the header() returns to the index.php page.

All that to say that the header() function can be used at the end of a script to refer to another script.
But BE CAREFUL if this is positioned at the start of the script, it does not prevent the script from running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文