1. 程式人生 > >PHP經典header錯誤"Cannot modify header information"的解決方法(轉)

PHP經典header錯誤"Cannot modify header information"的解決方法(轉)

錯誤提 示:Cannot modify header information - headers already sent by ....

昨晚在轉換編碼的時候,有一個 頁面需要在head中申明utf-8的編碼,但是這與程式中的一處header產生了衝突。google了一下,找到幾種解決方法,翻譯整理一下:

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 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執行效率,這取決於伺服器的效能和程式碼的複雜度。 

相關推薦

PHP經典header錯誤"Cannot modify header information"的解決方法

錯誤提 示:Cannot modify header information - headers already sent by .... 昨晚在轉換編碼的時候,有一個 頁面需要在head中申明utf-8的編碼,但是這與程式中的一處header產生了衝突。google了一下,找到幾種解決方法,翻譯整

PHPExcel導出大量數據超時及內存錯誤解決方法

導出excel 問題 provide eno 內存 們的 diff ini actor 轉自:http://lhdst-163-com.iteye.com/blog/2149653 PHP導出excel相對很多童鞋都碰到了,使用phpexcel類也確實方便,但導出大數據

PHP之緩存雪崩,及解決方法

mac 不同 發生 均勻分布 雙緩存 lte 目標 網站 本地緩存 一、什麽是緩存雪崩緩存雪崩就是指緩存由於某些原因(比如 宕機、cache服務掛了或者不響應)整體crash掉了,導致大量請求到達後端數據庫,從而導致數據庫崩潰,整個系統崩潰,發生災難。 下面的就是一個雪崩

HTTP 錯誤 500.21 - Internal Server Error 解決方案

san 原因 cal 方案 發生 err 檢查 net tle 不久前重新安裝了Windows7,在安裝了VS2010 開發平臺之後,將網站發布到IIS,訪問發生如下錯誤: HTTP 錯誤 500.21 - Internal Server Error處理程序“NickLee

php防止sql註入的方法

原來 nta puts post提交 支持 允許 line php代碼 開發人員 【一、在服務器端配置】 安全,PHP代碼編寫是一方面,PHP的配置更是非常關鍵。我們php手手工安裝的,php的默認配置文件在 /usr/local/apache2/conf/p

PHP curl後json_decode無法將json轉換成陣列

整了嗯久,PHP curl後json_decode無法將json轉換成陣列;最後才得到原因: curl返回的資料中帶有bom格式,需要轉換; 有些返回資料直接: print_r(json_decode($data,true));  就可以轉換。   

Android開發中遇到的錯誤解決方法1

(1)Android APK安裝後不在桌面生成快捷方式圖示  大家知道在Android系統的手機安裝APK應用後會在桌面上生成此APK的快捷方式,使用者通過直接點選可以進入此應用程式。但作為開發人員,我們有時候並不希望在安裝APK完成後生成快捷方式,而是通過先進入某個應用程

Read-only file system錯誤解決辦法

今天在公司釋出新版本時,釋出指令碼突然報Read-only file system的錯誤,之前一直還用的好好的…登入到遠端伺服器上,發現只要涉及到修改/儲存條目等需要寫磁碟操作的命令都無法使用(如tar、cp、mv、rm、chmod、chown、wget下載等指令),總是

ImportError: No module named XXX 錯誤解決方法Windows

window環境下使用pycharm編輯器來寫Python時,由from *** import *** 語句導致控制檯輸出ImportError: No module named ***時,顯而易見是由於未找到名為***的模組。遇到這類問題,首先要確認是否已經安裝過該模組。

PHP建立影象時成為亂碼的解決方法GD

有問題的程式碼: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/x

使用PHP+Swoole實現的網頁即時聊天工具:PHPWebIM

使用PHP+Swoole實現的網頁即時聊天工具 全非同步非阻塞Server,可以同時支援數百萬TCP連線線上 同時支援websocket+comet2種相容協議,可用於所有種類的瀏覽器包括IE 擁有完整的UI介面 支援單聊/群聊/組聊等功能 支援傳送表

ssh 登入時常出現的幾種錯誤以及解決方法Linux

前言 ssh是Linux系統中常用的遠端登陸的命令,有的時候我們通過xshell等遠端連線軟體使用ssh去登陸遠端的伺服器的時候,會遇到一些問題。 下面是關於ssh 遠端登陸的問題解決方法的總結。(更新中) 常見報錯及解決 1.SSH

PHP錯誤:Warning: Cannot modify header information

如果在執行php程式時看到這條警告:"Warning: Cannot modify header information - headers already sent by ...." Few notes based on the following user posts:有

PHP提示Cannot modify header information - headers already sent by解決方法

PHP提示Cannot modify header information - headers already sent by解決方法 因為 header();傳送頭之前不能有任何輸出,空格也不行, 需要將header()之前的空格去掉,或者其他輸出的東西去掉, 如果他上面include其他檔案了,你還

php 解決Warning: Cannot modify header information

環境:mac問題:瀏覽器出現 Warning: Cannot modify header information - headers already sent by...問題原因:函式 header()

php5.6,Ajax報錯,Warning: Cannot modify header information - headers already sent in Unknown on line 0

ont line span use -s nbsp ati bsp data php5.6ajax報錯 Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be r

Warning: Cannot modify header information解決辦法

昨天將本站的wordpress升級到最新版本,今天早上,經過兩位朋友的提醒,才發現用非chrome瀏覽器開啟部落格,頭部會顯示有錯誤,錯誤內容為: Warning: Cannot modify header information – headers already sent by (output

ajax 上傳檔案 報錯 Warning: Cannot modify header information - headers already sent in Unknown on line 0

在使用ajax更新或上傳資料的時候,return回來的卻是一堆錯誤 後來才知道原來是PHP5.6有的功能已經廢棄了,所以我需要開啟PHP.ini檔案,找到 ;always_populate_raw_post_data = -1 將前面的分號去掉 always_popula

解決Warning: Cannot modify header information

資訊的時候經常提示:cannot modify header information - headers already sent by (......)。其實已經實現需要的效果了,就是這個錯誤資訊看著不爽,網上找了很多辦法,綜合使用得到的解決方法是 1在頁面頂部的php標籤

【Visual Studio】解決錯誤 fatal error C1010: unexpected end of file while looking for precompiled head

set while err sin 結束 art fat 出現 using 原文轉自 http://blog.csdn.net/liuqiyao_01/article/details/38867145 在編譯VS時候,出現fatal error C1010: unexp