如何禁用“标头已发送” Linux、cpanel 上的消息?

发布于 2024-08-17 18:40:39 字数 283 浏览 1 评论 0原文

我在本地主机上构建我的网站(在 Windows 上运行 wamp),当我将其上传到我的服务器时,我总是得到 “无法修改标头信息 - 标头已发送”

我知道不应该有任何空行和所有内容,通常这可以解决。但现在我需要在发送标头后重定向某人,如何使我的服务器像我的本地主机一样工作?

我正在使用 cpanel 和 WHM:

cPanel 11.25.0-R42399 - WHM 11.25.0 - X 3.9 vps 上的 CENTOS 5.4 x86_64 virtuozzo

我将不胜感激

I building my sites on the localhost (runs wamp on windows), and when I upload it to my server, I always get
"Cannot modify header information - headers already sent"

I understand that there shouldn't be any blank lines and everyhing, and usually this works out. but now I need to redirect someone after the header has been sent, how can I make my server act like my localhost ?

i'm using cpanel and WHM:

cPanel 11.25.0-R42399 - WHM 11.25.0 - X 3.9
CENTOS 5.4 x86_64 virtuozzo on vps

I will appreciate any help

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

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

发布评论

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

评论(3

提赋 2024-08-24 18:40:39

简而言之,在使用 header() 函数之前,您需要阻止 PHP 向浏览器输出任何内容。

这应该通过仔细的编程实践来完成,其中“无空行”就是其中之一,或者将 PHP 的输出存储在输出缓冲区中,并且仅在准备好时才输出。

请参阅 ob_start()ob_flush() 方法。您可以在应用程序开始时使用 ob_start() 。这会禁用输出并将其存储到缓冲区中。当您准备好开始输出时,请使用 ob_flush(),PHP 会将缓冲区的内容发送到浏览器,包括此时设置的标头。如果您不调用 ob_flush() ,则缓冲区将在脚本末尾输出(刷新)。

它之所以能在您的 WAMP 开发环境中运行,很可能是因为 php.ini 中已经默认启用了输出缓冲。这些一体化软件包常常为前 4k 字节左右启用默认缓冲区。但是,通常最好在代码中显式启动并刷新缓冲区,因为这会强制执行更好的编码实践。

In short, you need to prevent PHP from outputting anything to the browser before you get to the point where you want to use the header() function.

This should be done by careful programming practices, of which your 'no blank lines' is one, or by storing PHP's output in an output buffer, and only outputting when you're ready for it.

See the ob_start() and ob_flush() methods. You use ob_start() at the start of your application. This disables output and stores it into a buffer. When you're ready to start outputting, use ob_flush() and PHP will send the buffer's contents to the browser, including the headers that are set till that point. If you don't call ob_flush() then the buffer is output (flushed) at the end of the script.

The reason why it works on your WAMP development environment is most likely that output buffering is already enable by default in the php.ini. Quite often these all-in-one packages enable a default buffer for the first 4k bytes or so. However, it is generally better to explicitly start and flush the buffer in your code, since that forces better coding practices.

漆黑的白昼 2024-08-24 18:40:39

出色地,
我想通过更多的思考和更好的编程,您可以在编写任何 HTML 之前设法保留所有重定向。

这个问题用旧规则解决了......

Well,
I guess by more thinking and better programing you can manage to keep all redirects before any HTML is written.

This problem solved by the old rules...

来世叙缘 2024-08-24 18:40:39

@user31279:我所知道的最快和最肮脏的方法是使用 @ 来抑制警告,所以例如

@header('Location: some-other-page.php');

@user31279: The quickest and dirtiest way I know of is to use @ to suppress the warning, so e.g.

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