php徹底根絕warning: Cannot add header information - headers already sent in.

來源:互聯網
上載者:User
php徹底杜絕warning: Cannot add header information - headers already sent in......

解決方案:

1)適用於有許可權編輯PHP.INI的人

開啟php.ini檔案(你應試比我清楚你的php.ini在哪裡),找到

output_buffering =改為on或者任何數字.如果是IIS6,請一定改為ON,不然你的PHP效率會奇慢.

2)使用虛擬機器主機,不能編輯PHP.INI,怎麼辦?

簡單:

在你的空間根目錄下建立一個.htaccess檔案,內容如下:

AllowOverride All
PHP_FLAG output_buffering On

不幸的情況是:還是不行?全部網頁都不能顯示啦?

那麼,你可以打電話罵一通空間商,然後讓他給你把apache的.htaccess AllowOverride開啟

3)在PHP檔案裡解決

ob_start()
啟用output buffering機制。 Output buffering支援多層次 -- 例如,可以多次調用 ob_start() 函數。

ob_end_flush()
發送output buffer(輸出緩衝)並禁用output buffering機制。

ob_end_clean()
清除output buffer但不發送,並禁用output buffering。

ob_get_contents()
將當前的output buffer返回成一個字串。允許你處理指令碼發出的任何輸出。

原理:

output_buffering被啟用時,在指令碼發送輸出時,PHP並不發送HTTP header。相反,它將此輸出通過管道(pipe)輸入到動態增加的緩衝中(只能在PHP 4.0中使用,它具有中央化的輸出機制)。你仍然可以修改/添加header,或者設定cookie,因為header實際上並沒有發送。當全部指令碼終止時,PHP將自動發送HTTP header到瀏覽器,然後再發送輸出緩衝中的內容。

轉載請一定註明來自:www.phpv.net

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.