如何禁用“标头已发送” Linux、cpanel 上的消息?
我在本地主机上构建我的网站(在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简而言之,在使用 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.
出色地,
我想通过更多的思考和更好的编程,您可以在编写任何 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...
@user31279:我所知道的最快和最肮脏的方法是使用
@
来抑制警告,所以例如@user31279: The quickest and dirtiest way I know of is to use
@
to suppress the warning, so e.g.