处理错误Cannot modify header information

2009-06-26  |  分类:网络技术资讯  |  标签:  |  344 views

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 script.
检查有<?php … ?> 后面没有空白行,特别是include或者require的文件。不少问题是这些空白行导致的。

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

3a. Use Javascript (用Javascript来解决):
<? echo “<script> self.location(\”file.php\”);</script>”; ?>
Since it’s a script, it won’t modify the header until execution of Javascript.
可以用Javascript来代替header。但是上面的这段代码我没有执行成功… 另外需要注意,采用这种方法需要浏览器支持Javascript.

3b. Use output buffering (用输出缓存来解决):
<?php ob_start(); ?>
… HTML codes …
<?php
… PHP codes …
header (”Location: ….”);
ob_end_flush();
?>

就像上面的代码那样,这种方法在生成页面的时候缓存,这样就允许在输出head之后再输出header了。

http://www.discuz.net/viewthread.php?tid=70318

在后台管理或者有时候在论坛,点击一个页面,页顶会出现
Warning: Cannot modify header information - headers already sent by….
这类语句,造成这个原因是因为setcookie语句的问题。
 这种方法和3b的方法理论上是一样的。

cookie本身在使用上有一些限制,例如:
1.呼叫setcookie的敘述必須放在<html>標籤之前
2.呼叫setcookie之前,不可使用echo
3.直到網頁被重新載入後,cookie才會在程式中出現
4.setcookie函數必須在任何資料輸出至瀏覽器前,就先送出
5.……
 

4.set output_buffering = On in php.ini (开启php.ini中的output_buffering )

◎ 如果喜欢本文,你可以 订阅本站全文,以便获取更多相关的信息。

◎ 转载本站文章请在文中注明「转载自 外贸网站建设 字样,并附上本站链接,敬请合作!

◎ 本文永久链接:http://www.weiblog.com/archives/528
喜欢本文,就收藏到: Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网

最新日志

  • No Related Post

发表您的评论

您必须 登录 才能发表评论。