php文件如果原来为ansi格式,html页面为uft-8,alert就会乱码。
但是如果将php文件直接改成utf-8则会出现Cannot send session cache limiter - headers already sent
解决方法为将原来php文件内容全删了,保存为utf-8格式,然后将原来内容复制进去 ok;
上面方法已爆炸
新方法
执行一个clearBOM.php在项目文件夹下
内容如下
<?php
$basedir = str_replace('/clearBOM.php','',str_replace('\\','/',dirname(__FILE__)));
$auto = 1;
checkdir($basedir);
function checkdir($basedir){
if ($dh = opendir($basedir)) {
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..'){
if (!is_dir($basedir.'/'.$file)) {
$filename = $basedir.'/'.$file;
echo 'filename:'.$basedir.'/'.$file.checkBOM($filename).'<br>';
} else {
$dirname = $basedir.'/'.$file;
checkdir($dirname);
}
}
}
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);
}
?>