、或「Cannot modify header information - headers already sent」setcookie() 無效Byte Order Mark (BOM) 是 UTF-16/UTF-32 標識位元組序的標記。UTF-8 雖無位元組序問題,但部分編輯器(如早期 Dreamweaver、Notepad)預設在 UTF-8 檔案開頭寫入 EF BB BF 三個位元組作為標識。
PHP 解析器將 BOM 視為輸出內容,導致「輸出已開始」,後續 header()、session_start()、setcookie() 失效。PHP 7.1+ 可自動忽略開頭 BOM,但舊版本或特定 SAPI 仍受影響。
File → File Encoding → UTF-8,取消「Add BOM」File → Save with Encoding → UTF-8編碼 → 轉為 UTF-8 (無 BOM)# .gitattributes 強制所有 PHP/HTML/JS/CSS 為無 BOM
*.php text eol=lf
*.html text eol=lf
*.js text eol=lf
*.css text eol=lf
*.xml text eol=lf
# 檢查專案中是否含 BOM
grep -r $'\xEF\xBB\xBF' --include="*.php" --include="*.html" .
# 或用 PHP 腳本掃描
php -r "foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')) as \$f) { if (\$f->isFile() && preg_match('/\.(php|html|xml|js|css)$/', \$f)) { \$c = file_get_contents(\$f); if (substr(\$c, 0, 3) === '\xEF\xBB\xBF') echo \$f->getPathname() . PHP_EOL; } }"
<?php
// 放在入口檔最頂端(
<?php 開頭,前面零字元。