php 办理Warning: Cannot modify header information headers a

问题:欣赏器呈现 Warning: Cannot modify header information - headers already sent by...

问题原因:函数 header(),setcookie() 和 session 函数需要在输出流中增加头信息。可是头信息只能在其它任何输出内容之前发送。在利用这些函数前不能有任何(如 HTML)的输出。函数 headers_sent() 可以或许查抄您的剧本是否已经发送了头信息。请参阅“输出节制函数”。 意思是:不要在利用上面的函数前有任何文字,空行,回车,空格等。

办理方案:修改php.ini 文件中 output_buffering 设为 on,然后重启 appache;

详细步调:终端呼吁

1、进入目次:cd /etc/

2、复制文件:cp php.ini.default php.ini

3、编辑修改文件:sudo vi php.ini

4、输入 i 执行编辑,输入output_buffering=on

5、esc 退出编辑

6、输入 :wq 生存退出

7、重启处事器 sudo apachectl restart

8、从头刷新php文件页面。问题办理!
————————————————

假如在执行php措施时看到这条告诫:"Warning: Cannot modify header information - headers already sent by ...."

Few notes based on the following user posts:
有以下几种办理要领:

1. Blank lines (空缺行):
Make sure no blank line after <?php ... ?> of the calling php scrīpt.
查抄有<?php ... ?> 后头没有空缺行,出格是include可能require的文件。不少问题是这些空缺行导致的。

2. Use exit statement (用exit来办理):
Use exit after header statement seems to help some people
在header后加上exit();
header ("Location: xxx");
exit();

3. PHP has this annoying problem, if your HTML goes before any PHP code or any header modification before redirecting to certain page, it ll said "Warning: Cannot modify header information - headers already sent by ...." Basically anytime you output to browser, the header is set and cannot be modified. So two ways to get around the problem:

3a. Use Javascrīpt (用Javascrīpt来办理):
<? echo "<scrīpt> self.location( file.php );</scrīpt>"; ?>
Since it s a scrīpt, it won t modify the header until execution of Javascrīpt.
可以用Javascrīpt来取代header。别的需要留意,回收这种要领需要欣赏器支持Javascrīpt.

3b. Use output buffering (用输出缓存来办理):
<?php ob_start(); ?>
... HTML codes ...
<?php
... PHP codes ...
header ("Location: ....");
ob_end_flush();
?>
This will save the output buffer on server and not output to browser yet, which means you can modify the header all you want until the ob_end_flush() statement. This method is cleaner than the Javascrīpt since Javascrīpt method assumes the browser has Javascrīpt turn on. However, there are overhead to store output buffer on server before output, but with modern hardware I would imagine it won t be that big of deal. Javascrīpt solution would be better if you know for sure your user has Javascrīpt turn on on their browser.

就像上面的代码那样,这种要领在生成页面的时候缓存,这样就答允在输出head之后再输出header了。本站的许愿板就是回收这种要领办理的header问题。

4.set output_buffering = On in php.ini (开启php.ini中的output_buffering )
set output_buffering = On will enable output buffering for all files. But this method may slow down your php output. The performance of this method depends on which Web server you re working with, and what kind of scrīpts you re using.
这种要领和3b的要领理论上是一样的。可是这种要领开启了所有php措施的输出缓存,这样做大概影响php执行效率,这取决于处事器的机能和代码的巨大度。

第二种:

如何彻底杜绝warning: Cannot add header information - headers already sent in…… 这种令人莫明其妙的的错误。 只要你写过PHP代码,相信都赶上过这个大多时候都令人莫明其妙的warning吧..本日我们就来搞定它…………… 看了PHP手册,答复如下:动静“Warning: Cannot send session cookie - headers already sent…”可能“Cannot add/modify header information - headers already sent…”。 函数 header(),setcookie() 和 session 函数需要在输出流中增加头信息。可是头信息只能在其它任何输出内容之前发送。在利用这些函数前不能有任何(如 HTML)的输出。函数 headers_sent() 可以或许查抄您的剧本是否已经发送了头信息。请参阅“输出节制函数”。 意思是:不要在利用上面的函数前有任何文字,空行,回车,空格等。但。。。问题是,这谜底并不令人满足。因为往往措施在其他PHP情况下运行却正常。 首先:这错误是怎么发生的呢?让我们来看看PHP是如那里理惩罚HTTP header输出和主体输
文接头的是如何彻底杜绝warning: Cannot add header information - headers already sent in…… 这种令人莫明其妙的的错误。
只要你写过PHP代码,相信都赶上过这个大多时候都令人莫明其妙的warning吧..本日我们就来搞定它……………
看了PHP手册,答复如下:
动静“Warning: Cannot send session cookie - headers already sent…”可能“Cannot add/modify header information - headers already sent…”。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/7709.html