php中header無法跳轉

高洛峰
發布: 2023-03-03 16:46:02
原創
3322 人瀏覽過

解決Warning:cannot modify header information - headers already sent by......

資訊的時候常常提示: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 fwingwing user posts: 
有下列幾種解法:

1. Blank lines (空白行): 
Make sure no blank line after of the calling php script. 
檢查有 後面沒有空白行,特別是include或require的檔案。不少問題是這些空白行所導致的。

2. Use exit statement (用exit來解決): 
Use exit after header statement seems to help some people 
在header後面加上exit(); 
header ("$xxx);

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 redirecting to certain page, it'll said "Warning: Cannot redirecting to certain page, it'll said "Warning: Cannot redirecting to certain page, it'll said "Warning: Cannot modify. anytime you output to browser, the header is set and cannot be modified.   So two ways to get around the problem:

3a. Use一天modify the header until execution of Javascript. 

可以用Javascript來取代header。但是上面的這段程式碼我沒有執行成功... 另外需要注意,採用這種方法需要瀏覽器支援Javascript.

3b. Use output buffering (用輸出快取來解決): 

<? echo "<script> self.location(\"file.php\");</script>"; ?>
登入後複製

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 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 你

就像上面的程式碼一樣,這種方法在產生頁面的時候會緩存,這樣就允許在輸出head之後再輸出header了。本站的許願板就是採用這種方法解決的header問題。 
在後台管理或有時候在論壇,點選一個頁面,頁頂會出現 

Warning: Cannot modify header information - headers already sent by.... 

這類語句,造成這個原因是因為setcookie語句的問題。

cookie本身在使用上有一些限制,例如: 

1.呼叫setcookie的敘述必須放在標籤之前 
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 downod may is 3. 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 = &#39;filetest.txt&#39;;//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(&#39;filetest.txt&#39;)); 
flush(); // this doesn&#39;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必须在标签之前 
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的文章 

更多php中header无法跳转相关文章请关注PHP中文网!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!