Cannot modify header information - headers already sent by ...
在 setcookie() 前輸出任何內容(含空白字符)都會觸發此錯誤。但明明代碼頂部無輸出,為何仍報錯?
ob_start() + ob_get_clean() 捕獲輸出 → 發現輸出為空字符串bomb 選項開啟:set nobomb → 問題解決Byte Order Mark (BOM):文件開頭的隱藏字節標記編碼(UTF-8 為 EF BB BF)。
:set nobomb → 保存<?php
// 入口文件最頂部
if (substr($content, 0, 3) === pack('CCC', 0xEF, 0xBB, 0xBF)) {
$content = substr($content, 3);
}
// 或使用 ob_start() 緩衝輸出
ob_start();
?>
grep -r $'\xEF\xBB\xBF' .一個隱形的 BOM,能讓整個項目集體「頭痛」。養成無 BOM 習慣,是 PHP 開發者的基本功。