Remove BOM header in PHP

Source: Internet
Author: User
Tags blank page

 

BOM: Byte Order Mark
UTF-8 BOM is also called the UTF-8 signature, in fact, the BOM of the UTF-8 has no effect on the UFT-8, is to support the UTF-16, the BOM is added to the UTF-32, the BOM signature is to tell the editor of the current file using what encoding, it is easy for the editor to identify, but BOM is not displayed in the editor, but it will generate output, just like an empty line,

If you occur after modifying any PHP file:

* You cannot log on or log out. * a blank page is displayed on the top of the page. * an error warning is displayed on the top of the page. * other abnormal situations are displayed.

 

Most of them are editor problems.

This program uses UTF-8 encoding. Now, almost all text editing software can display and edit UTF-8-encoded files. However, unfortunately, many of them are not doing well.

Similar to WINDOWS notepad and other software, when saving a file encoded in UTF-8, it inserts three invisible characters (0xEF 0xBB 0xBF, BOM) at the beginning of the file ). It is a string of hidden characters, used for the notepad editor to identify whether the file is encoded in UTF-8. For general files, this will not cause any trouble. However, BOM is a big headache for PHP.

PHP does not ignore the BOM. Therefore, when reading, including, or referencing these files, the BOM is used as part of the Beginning body of the file. According to the characteristics of the embedded language, this string of characters will be directly executed (displayed. As a result, even if the top padding of the page is set to 0, the whole web page cannot be placed close to the top of the browser, because there are three characters at the beginning of html!

This is not the biggest problem. Due to restrictions of the COOKIE sending mechanism, Cookies cannot be sent to files with BOM at the beginning of these files (because PHP has already sent the file header before the COOKIE is sent ), therefore, the logon and logout functions are invalid. All functions dependent on cookies and sessions are invalid.

Therefore, when editing or changing any text files, you must use an editor without adding BOM. The editor in Linux should have no such problem. In WINDOWS, do not use notepad or other editors. Recommended editors are: Editplus version 2.12 and later; EmEditor; UltraEdit (options related to 'add BOM 'need to be removed); Dreamweaver (options related to 'add BOM' need to be removed.

If you want to cancel a file that has been added with BOM, you can use the editor above to save it again. (Editplus needs to be saved as gb first, then as UTF-8 .) , The following is a program solution:

Tags:<None> code snippet (1)
<?php$auto = 1;checkdir('C:\project\weibo');function checkdir($basedir){if ($dh = opendir($basedir)) {  while (($file = readdir($dh)) !== false) {  if($file{0} == '.')  {  continue;  }   if ($file != '.' && $file != '..'){    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);}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.