search
Homephp教程PHP源码PHP remove BOM header

PHP remove BOM header

Nov 08, 2016 pm 05:25 PM

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.