wrodpress 错误提示"Cannot modify header information - headers already sent",可恶的utf8 BOM

wordpress换个服务器测试经常会出现错误,提示Cannot modify header information - headers already sent by (.../wp-config.php) in ... 这个错误提示的意思其实是有多个header()输出。但是仅仅是改了wp-config.php的配置,没有改其他东西,就会出现这个问题。网上说的一些原因
session.auto_start = 1 // 0 为不自动打开,请设为1 session.save_path= /tmp //对此目录是否有写权 把php.ini中output_buffering = off改为output_buffering = on
但是这些服务器都配置好了,不是这些原因。 问题还是出在wp-config.php这个文件上。 这个文件的php标签<?php 和?>外边不能有空白字符。当用一些编辑器编辑过之后,就会添加utf-8 BOM (Byte Order Mark), 即utf-8标签。这样就会在文件的开头添加一个输出,导致上面的问题。 在windows下有一些编辑器可以保存时选择无BOM(如editplus),这样重新保存一下文件,选择无BOM,然后替换原来的文件,就可以解决问题。 网上有用php来删除BOM的代码。将代码保存为php文件,放在某目录下,运行一下就可以删除当前目录(不包含子目录)下文件的BOM。当然文件权限要允许写入。下面是代码:
php删除BOM
<?php
//此文件用于快速测试UTF8编码的文件是不是加了BOM,并可自动移除
$basedir="."; //修改此行为需要检测的目录,点表示当前目录
$auto=1; //是否自动移除发现的BOM信息。1为是,0为否。
//以下不用改动
if ($dh = opendir($basedir)) {
while (($file = readdir($dh)) !== false) {
if ($file!='.' && $file!='..' && !is_dir($basedir."/".$file)) echo "filename: $file ".checkBOM("$basedir/$file")." <br>";
}
closedir($dh);
}
function checkBOM ($filename) {
global $auto;
$contents=file_get_contents($filename);
$charset[1]=substr($contents, 0, 1);
$charset[2]=substr($contents, 1, 1);
$charset[3]=substr($contents, 2, 1);
if (ord($charset[1])==239 && ord($charset[2])==187 && ord($charset[3])==191) {
if ($auto==1) {
$rest=substr($contents, 3);
rewrite ($filename, $rest);
return ("<font color=red>BOM found, automatically removed.</font>");
} else {
return ("<font color=red>BOM found.</font>");
}
}
else return ("BOM Not Found.");
}
function rewrite ($filename, $data) {
$filenum=fopen($filename,"w");
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}
//结束
?>
posted @ 2010-06-05 14:03  leetom  阅读(294)  评论(0编辑  收藏  举报