PHP Removal BOM Header code
UTF-8 BOM is also called UTF-8 signature, in fact, UTF-8 BOM to UFT-8 has no effect, is to support utf-16,utf-32 to add the Bom,bom signature means to tell the editor what encoding the current file, convenient editor identification, But although the BOM does not appear in the editor, it produces output, like a blank line.
If you modify any PHP file after it occurs:
* Cannot log in or log out;
* A blank appears at the top of the page;
* ERROR warning on top of page;
* Other abnormal conditions.
This program uses UTF-8 encoding. Now almost all text editing software can display and edit UTF-8 encoded files. Unfortunately, many of these software performance is not ideal.
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 * annoying.
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.
Therefore, when editing or changing any text file, be sure to use an editor that does not mess with the BOM. The editor under Linux should not have this problem. Under Windows, do not use editors such as Notepad. Recommended editors are: EditPlus 2.12, EmEditor, UltraEdit (need to cancel the ' Add BOM ' option), Dreamweaver (need to cancel ' add BOM ' related options), etc.
For a file that has already been added to the BOM, you can save it with the above editor if you want to cancel it. (EditPlus needs to be saved as GB first and then saved as UTF-8.) ), the following is the program solution:
if (Isset ($_get[' dir '))) {//Set file directory $basedir = $_get[' dir '];} else {$basedir = '. ';} $auto = 1;checkdir ($basedir), function Checkdir ($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"). " ";} 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 ("BOM removed.");} else {return ("BOM found.");}} Elsereturn ("BOM not Found.");} function rewrite ($filename, $data) {$filenum = fopen ($filename, "w"); Flock ($filenum, lock_ex); FwriTe ($filenum, $data); fclose ($filenum);}