Home >php教程 >PHP源码 >PHP remove BOM header

PHP remove BOM header

大家讲道理
大家讲道理Original
2016-11-08 17:25:181443browse

BOM: Byte Order Mark UTF-8 BOM is also called UTF-8 signature, actually UTF-8 The BOM has no effect on UFT-8, it is to support UTF-16 , the BOM added to UTF-32, the BOM signature means to tell the editor what encoding the current file uses, which is convenient for the editor to identify. However, although the BOM is not displayed in the editor, it will produce output, just like one more A blank line, if after you modify any PHP file: Cannot log in or log out; * A blank line appears at the top of the page; * An error warning appears at the top of the page; Other abnormal situations. It's probably an editor problem.

This program uses UTF-8 encoding. Almost all text editing software now can display and edit UTF- 8 encoded files. But unfortunately, many of them don't perform well. Software such as Notepad that comes with WINDOWS, when saving a file in UTF- 8-encoded file, three invisible characters (0xEF 0xBB 0xBF, or BOM) will be inserted at the beginning of the file. it is a string of hidden characters , used to let editors such as Notepad identify whether this file is encoded in UTF-8. ​​​

​​​​​​​​For ordinary files, this will not cause any trouble. But for For PHP, BOM is a big trouble. PHP does not ignore BOM , so when reading, including or referencing these files, the BOM will be used as part of the beginning text of the file. According to the characteristics of embedded languages, this string of characters will be executed (displayed) directly. As a result, even if the page top padding Setting it to 0 will not allow the entire web page to stick to the top of the browser, because there are these 3 characters at the beginning of the html! The biggest trouble is not this. Subject to the limitations of the COOKIE sending mechanism, in For files that already have a BOM at the beginning of these files, the COOKIE cannot be sent (because PHP has already sent the file header before the COOKIE is sent), so the login and logout functions are invalid. Everything relies on COOKIE and SE All functions implemented by SSION are invalid.​ ​

​ ​ ​ Therefore, when editing or changing any text file, please be sure to use an editor that does not add BOM randomly. Editors under Linux should not have this problem. Under WINDOWS, Do not use editors such as Notepad. The recommended editors are: Editplus version 2.12 or above; EmEditor; UltraEdit (need to cancel the related options of ‘Add BOM’); Dreamweaver (need to cancel the related options of ‘Add BOM’) wait. For files that have added BOM, if you want to cancel, you can use the above editor to save it once. (Editplus needs to save as gb first, then save as UTF-8.) , the following is the program solution:

[PHP] code

<?php
 
$auto = 1;
checkdir(&#39;C:\project\weibo&#39;);
function checkdir($basedir){
if ($dh = opendir($basedir)) {
  while (($file = readdir($dh)) !== false) {
    if($file{0} == &#39;.&#39;)
    {
        continue;
    }
   if ($file != &#39;.&#39; && $file != &#39;..&#39;){
    if (!is_dir($basedir."/".$file)) {
     echo "filename: $basedir/$file ".checkBOM("$basedir/$file")." <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);
}
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn