Original link: http://www.cnblogs.com/Athrun/archive/2010/05/27/1745464.html
Another article: " about bom.php", http://hi.baidu.com/aullik5/blog/item/f0e589127a28a2f0f7039e5e.html
Another article: "The solution of the UTF8 mystery coding problem of the [bom]php program], http://www.mlecms.com/tech/56.html
Software such as Windows-brought Notepad, when saving a UTF-8 encoded file, inserts three invisible characters (0xEF 0xBB 0xBF, or BOM) where the file begins. It is a string of hidden characters that allows editors such as Notepad to identify whether the file is encoded in UTF-8. For general files, this does not cause any trouble. But for PHP, BOM is a big hassle.
PHP does not ignore the BOM, so when you read, include, or reference these files, the BOM is used as part of the text at the beginning of the file. Depending on the characteristics of the embedded language, this string of characters will be executed directly (shown). As a result, even if the top padding of the page is set to 0, there is no way to keep the entire page close to the top of the browser, because there are 3 characters at the beginning of the HTML!
The biggest trouble is not this yet. Limited by the cookie-sending mechanism, where the file has a BOM at the beginning of these files, the cookie cannot be sent out (because PHP has sent the file header before the cookie is sent), so the login and logout functions fail. All the functions that rely on cookies and session implementations are not valid.
<?PHP/*detect and Clear BOM*/ if(isset($_get[' dir '])){ $basedir=$_get[' dir ']; }Else{ $basedir= '. '; } $auto= 1; Checkdir ($basedir); functionCheckdir ($basedir){ if($DH=Opendir($basedir)){ while(($file=Readdir($DH)) !==false){ if($file! = '. ' &&$file! = ' ... '){ if(!Is_dir($basedir." /".$file)){ Echo"FileName:$basedir/$file". Checkbom ("$basedir/$file")." <br> "; }Else{ $dirname=$basedir." /".$file; Checkdir ($dirname); } } }//End While Closedir($DH); }//End If ($dh}//End FunctionfunctionCheckbom ($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.")); }//End FunctionfunctionRewrite$filename,$data){ $filenum=fopen($filename, "W"); Flock($filenum,lock_ex); fwrite($filenum,$data); fclose($filenum); }//End Function?>
Complete