PHP remove BOM header

WBOY
Release: 2016-07-25 08:42:14
Original
832 people have browsed it

BOM: Byte Order Mark UTF-8 BOM is also called UTF-8 signature. In fact, the BOM of UTF-8 has no effect on UFT-8. It is added to support UTF-16 and UTF-32. The meaning of BOM signature is It just tells the editor what encoding the current file uses to facilitate the editor's identification. However, although the BOM is not displayed in the editor, it will produce output, just like an extra blank line. If you modify any PHP file, it will happen: Unable to 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 will insert three invisible characters (0xEF 0xBB 0xBF, or BOM) at the beginning of the file when saving a file encoded in UTF-8. It is a string of hidden characters used to let editors such as Notepad identify whether the file is encoded in UTF-8. For ordinary files, this will not cause any trouble. But for PHP, BOM is a problem. PHP does not ignore the 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 top padding of the page is set to 0, the entire web page cannot be placed close to the top of the browser, because there are these 3 characters at the beginning of the html! The biggest trouble is not this. Due to the limitations of the COOKIE sending mechanism, in 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. All functions that rely on COOKIE and SE SSION are invalid. Therefore, when editing or changing any text file, be sure to use an editor that does not add BOM randomly. Editors under Linux should not have this problem. Under WINDOWS, please do not use editors such as Notepad. The recommended editors are: Editplus version 2.12 or above; EmEditor; UltraEdit (the relevant options of 'Add BOM' need to be cancelled); Dreamweaver (the relevant options of 'Add BOM' need to be cancelled), etc. 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, and then save as UTF-8.) , the following is the program solution:

[PHP] code

  1. $auto = 1;
  2. checkdir('C:projectweibo');
  3. function checkdir($basedir){
  4. if ($dh = opendir($basedir)) {
  5. while (( $file = readdir($dh)) !== false) {
  6. if($file{0} == '.')
  7. {
  8. continue;
  9. }
  10. if ($file != '.' && $file ! = '..'){
  11. if (!is_dir($basedir."/".$file)) {
  12. echo "filename: $basedir/$file ".checkBOM("$basedir/$file")." < ;br>";
  13. }else{
  14. $dirname = $basedir."/".$file;
  15. checkdir($dirname);
  16. }
  17. }
  18. }
  19. closedir($dh);
  20. }
  21. }
  22. function checkBOM ($filename) {
  23. global $auto;
  24. $contents = file_get_contents($filename);
  25. $charset[1] = substr($contents, 0, 1);
  26. $charset[2] = substr($contents, 1, 1);
  27. $charset[3] = substr($contents, 2, 1);
  28. if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord ($charset[3]) == 191) {
  29. if ($auto == 1) {
  30. $rest = substr($contents, 3);
  31. rewrite ($filename, $rest);
  32. return ("< font color=red>BOM found, automatically removed.");
  33. } else {
  34. return ("BOM found.");
  35. }
  36. }
  37. else return ("BOM Not Found.");
  38. }
  39. function rewrite ($filename, $data) {
  40. $filenum = fopen($filename, "w");
  41. flock($filenum, LOCK_EX);
  42. fwrite( $filenum, $data);
  43. fclose($filenum);
  44. }
  45. ?>
Copy code
PHP, BOM


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!