hp函數setcookie()報錯:Warning: Cannot modify header_PHP教程

來源:互聯網
上載者:User
今天在利用php函數setcookie()設定值是提示:Warning: Cannot modify header information headers already sent by錯了,立即去gg問了一下,找到N種解決辦法下面我來整理一下希望對大家會有所協助。

快要下班的時候,看到php討論學習群中有朋友說設定cookie的時候。向他要了代碼看了原因!報錯
Warning: Cannot modify header information – headers already sent by (output started at cookie1.php:4) in cookie1.php on line 5


ob_start();
setcookie("username","宋岩賓",time()+3600);
echo "the username is:".$HTTP_COOKIE_VARS["username"]." ";
echo "the username is:".$_COOKIE["username"]." ";
print_r($_COOKIE);
?>
Warning: Cannot modify header information - headers already sent by出錯的原因
我在php程式的頭部加了,
header("cache-control:no-cache,must-revalidate");
之後頁面就出現上面的錯誤,看了N個資料也沒有結果。今天偶爾發現原來是我的php.ini裡面的配置出了問題,在C:windows下找到php.ini檔案
output_buffering預設為off的。我現在把它設為4096就OK了。
用於解決顯示提示錯誤,不能按(日期+匯出檔案數)為檔案名稱的錯誤資訊.
setcookie函數必?在任何?料?出至瀏覽器前,就先送出
基於上面?些限制,所以?絛?etcookie()函??r,常??齙?quot;Undefined index"、"Cannot modify header information - headers already sent by"…等???,解?Q"Cannot modify header information - headers already sent by"?????的方法是在?生cookie前,先延??料?出至?g?器,因此,您可以在程式的最前方加上ob_start();???函?怠?br /> ob_start()函數用於開啟緩衝區,比如header()函數之前如果就有輸出,包括斷行符號空格換行都會有"Header had all ready send by"的錯誤,這時可以先用ob_start()開啟緩衝區PHP代碼的資料區塊和echo()輸出都會進入緩衝區而不會立刻輸出.當然開啟緩衝區的作用很多,只要發揮你的想象.可以總結以下四點:

1.用於header()之前

ob_start(); //開啟緩衝區
echo "Hellon"; //輸出
header("location:index.php"); //把瀏覽器重新導向到index.php
ob_end_flush();//輸出全部內容到瀏覽器
?>

2.phpinfo()函數可擷取用戶端和伺服器端的資訊,但要儲存用戶端資訊用緩衝區的方法是最好的選擇.
ob_start(); //開啟緩衝區
phpinfo(); //使用phpinfo函數
$info=ob_get_contents(); //得到緩衝區的內容並且賦值給$info
$file=fopen('info.txt','w'); //開啟檔案info.txt
fwrite($file,$info); //寫入資訊到info.txt
fclose($file); //關閉檔案info.txt
?>

3.靜態頁面技術
ob_start();//開啟緩衝區
?>
php頁面的全部輸出
$content = ob_get_contents();//取得php頁面輸出的全部內容
$fp = fopen("output00001.html", "w"); //建立一個檔案,並開啟,準備寫入
fwrite($fp, $content); //把php頁面的內容全部寫入output00001.html,然後……
fclose($fp);
?>

4.輸出代碼
Function run_code($code) {
If($code) {
ob_start();
eval($code);
$contents = ob_get_contents();
ob_end_clean();
}else {
echo "錯誤!沒有輸出";
exit();
}
return $contents;
}

看了PHP手冊和搜尋了原因。得到一下結論

方法一:

在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 ……有人說要在檔案開頭寫上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 。

http://www.bkjia.com/PHPjc/633185.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/633185.htmlTechArticle今天在利用php函數setcookie()設定值是提示:Warning: Cannot modify header information headers already sent by錯了,立即去gg問了一下,找到N種解決辦法下面...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.