解決Warning: Cannot modify header information - headers already sent b...

信息的時候經常提示:cannot modify header information - headers already sent by (......)。其實已經實現需要的效果了,就是這個錯誤信息看着不爽,網上找了很多辦法,綜合使用得到的解決方法是

1在頁面頂部的php標籤中加入ob_start();

2在返回的信息下面加入ob_end_flush();

這樣就可以屏蔽錯誤信息的現實了

另外轉一下其他人的方法,也許在其他情況下也會有效

If you got this message: "Warning: Cannot modify header information - headers already sent by ...."
如果在執行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 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();

 

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 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();
?>
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 Javascript since Javascript method assumes the browser has Javascript 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.   Javascript solution would be better if you know for sure your user has Javascript turn on on their browser.

就像上面的代碼那樣,這種方法在生成頁面的時候緩存,這樣就允許在輸出head之後再輸出header了。本站的許願板就是採用這種方法解決的header問題。

後臺 管理或者有時候在論壇 ,點擊一個頁面 ,頁頂會出現
Warning: Cannot modify header information - headers already sent by....
這類語句,造成這個原因是因爲setcookie語句的問題。

cookie本身在使用上有一些限制,例如:
1.呼叫setcookie的敘述必須放在<html>標籤之前
2.呼叫setcookie之前,不可使用echo
3.直到網頁被重新載入後,cookie才會在程式中出現
4.setcookie函數必須在任何資料輸出至瀏覽器前,就先送出
5.……
基於上面這些限制,所以執行setcookie()函數時,常會碰到"Undefined index"、"Cannot modify header information - headers already sent by"…等問題,解決"Cannot modify header information - headers already sent by"這個錯誤的方法是在產生cookie前,先延緩資料輸出至瀏覽器,因此,您可以在程式的最前方加上ob_start();這個函數 。這樣就可以解決 了。

 

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 scripts you're using.
這種方法和3b 的方法理論上是一樣的。但是這種方法開啓了所有php程序的輸出緩存,這樣做可能影響php執行效率,這取決於服務器的性能和代碼的複雜度。

 

 

昨天想用PHP寫一段下載文件的代碼,因爲不想得怎麼設置HTTP協議就直接到php.net上找header()函數的事例,很多代碼,我直接拷貝了一段,

 

 

<? php
$file = ' filetest.txt ' ;// filetest.txt 文件你隨便寫點東西進去就好了
header ( " Content-Disposition: attachment; filename= " . urlencode ( $file ));   
header ( " Content-Type: application/force-download " );
header ( " Content-Type: application/octet-stream " );
header ( " Content-Type: application/download " );
header ( " Content-Description: File Transfer " );            
header ( " Content-Length: " . filesize ( ' filetest.txt ' ));
flush (); // this doesn't really matter.

$fp = fopen ( $file , " r " );
while ( ! feof ( $fp ))
{
    
echo fread ( $fp , 65536 );
    
flush (); // this is essential for large downloads
}

fclose ( $fp );

?>

運行了一下發現不行,一直報錯:Warning : Cannot modify header information - headers already sent by (output started at E:/xampp/htdocs/test/downloadfile/file_download.php:1) in E:/xampp/htdocs/test/downloadfile/file_download.php on line 3

我很看了很久,文件一開始就直接是header代碼了,沒任何輸出怎麼會說已有字符輸出了呢?後來上網查到別人給的提示,才發現,原來我創建文件的時候是直接用記事本存儲爲UTF8, 原來這樣也會出錯

----------------以下是引用他人的建議 --------------------

方法一:
在PHP裏Cookie的使用是有一些限制的。
1、使用setcookie必須在<html>標籤之前
2、使用setcookie之前,不可以使用echo輸入內容
3、直到網頁被加載完後,cookie纔會出現
4、setcookie必須放到任何資料輸出瀏覽器前,才送出
.....
由於上面的限制,在使用setcookie()函數時,學會遇到 "Undefined index"、"Cannot modify header information - headers already sent by"…等問題,解決辦法是在輸出內容之前,產生cookie,可以在程序的最上方加入函數 ob_start();

ob_start :打開輸出緩衝區
函數格式:void ob_start(void)
說明:當緩衝區激活時,所有來自PHP程序的非文件頭信息均不會發送,而是保存在內部緩衝區。爲了輸出緩衝區的內容,可以使用ob_end_flush()或flush()輸出緩衝區的內容。


方法二:
解 決Warning: Cannot modify header information - headers already sent by ...... 前幾天裝了個php的大頭貼系統測試,發現報錯Warning: Cannot modify header information - headers already sent by ......
今天又裝openads,還是出現這個問題。怒了。上網找了半天,有人說要在文件開頭寫上
ob_start();
失敗。
後來打開 php.ini 然後把 output_buffering 設爲 on 。重起appache,OK。看來這纔是解決辦法。

特別注意: (我就是看了這個才解決問題的)
如果使用utf-8編碼,一定要去掉UTF-8中的BOM,這都是因爲utf-8編碼文件含有的bom原因,而php4,5都是不支持bom的。去掉bom,可以用Notepad++打開轉換一下。(我就是看了這個才解決問題的)

用PHP的ob_start(); 控制您的瀏覽器cache 。我另外單獨轉載了一篇文章關於用PHP的ob_start();控制您的瀏覽器cache的文章

 

----------------END --------------------

來自: http://hi.baidu.com/apxsoft/blog/item/c0640dd3e66697093bf3cf77.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章