Yep, it’s a hell if you’ve got the UTF8 BOM (byte order mark) at the beginning of your PHP or XML files. These files need to send their own headers before anything else. Because of the BOM’s location, which is the first byte of the file, headers can not be received by browsers and unintented errors might occur.
For PHP the error will be “Warning: Cannot modify header information”, and for XML, “XML declaration allowed only at the start of the document”. If you are having header errors in your WordPress (including admin pages), it is most probably caused by a byte order mark in your theme files (First check : functions.php
)
How you can find and delete the BOM from text files ? Most frameworks and editors include a setting for saving non BOM UTF8 files. Check your help file, or ask at the forum or helpdesk of the tool you are using.
Second, never use Notepad on Windows for development purposes. It directly inserts the BOM when you save your file in UTF8 format.
If you are dealing with hundreds of files, finding and deleting the BOM is time consuming. So here is a PHP file I wrote for finding the files that you’ll need to correct.
What this script does is actually check all files’ first bytes for byte order mark characters by recursively moving around your home folder and subfolders. Every subfolder and file is checked and reported. After the script ends, it will also give you a small list of files that have BOM…
You can also find this file at http://emrahgunduz.com/BOM.php.txt as this will be easier, I hope
. Simply change the $HOME line to your home directory. If you are hosted on a Windows based machine, do not forget to change the $WIN line to 1, as Windows recursive find needs a different set of slashes.
Good luck on BOM. If you want to ask anything, please use comments.
PS. Delete the file after usage. This one prints the file list of your domain’s root and subfolders.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | <?php //Tell me the root folder path. //You can also try this one (not recommended for Windows hosts) //$HOME = $_SERVER["DOCUMENT_ROOT"]; $HOME = "/home/somewhere"; // Is this server is a Windows OS Host ? // If it is, change this line to $WIN = 1; $WIN = 0; //If it is not WINDOWS, keep it 0 ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>UTF8 BOM FINDER</title> <style> body { font-size: 10px; font-family: Arial, Helvetica, sans-serif; background: #FFF; color: #000; } .FILE { color: #0C0; } .DIR { color: #03C; font-style: italic; } .BOM { color: #F30; font-weight: bold; } .FOUND { color: #F30; font-size: 14px; font-weight: bold; } </style> </head> <body> <?php CheckFilesForBOM($WIN); $BOMBED = array(); function CheckFilesForBOM($win32=0) { global $HOME, $BOMBED; $win32 = ($win32 == 1) ? "\\" : "/"; $folder = dir($HOME); $foundfolders = array(); while ($file = $folder->read()) { if($file != "." && $file != "..") { if(filetype($HOME . $win32 . $file) == "dir"){ echo '<span class="DIR">'.$HOME . $win32 . $file." (DIR)</span><br />\n"; $foundfolders[count($foundfolders)] = $HOME . $win32 . $file; } else { $BOM = SearchBOM(file_get_contents($HOME . $win32 . $file)); if ($BOM) { $BOMBED[count($BOMBED)] = $HOME . $win32 . $file; echo '<span class="BOM">'.$HOME . $win32 . $file."</span><br />\n"; } else { echo '<span class="FILE">'.$HOME . $win32 . $file."</span><br />\n"; } } } } $folder->close(); if(count($foundfolders) > 0) { foreach ($foundfolders as $folder) { RecursiveFolder($folder, $win32); } } //Ending... echo '<br /><br /><hr /><h2>These files have UTF8 BOM:</h2><br />' .'<p class="FOUND">'; foreach ($BOMBED as $utf) { echo $utf ."<br />\n"; } echo '</p>'; } function RecursiveFolder($sHOME,$win32) { global $HOME, $BOMBED; $win32 = ($win32 == 1) ? "\\" : "/"; $folder = dir($sHOME); $foundfolders = array(); while ($file = $folder->read()) { if($file != "." && $file != "..") { if(filetype($sHOME . $win32 . $file) == "dir"){ echo '<span class="DIR">'.$HOME . $win32 . $file." (DIR)</span><br />\n"; $foundfolders[count($foundfolders)] = $sHOME . $win32 . $file; } else { $BOM = SearchBOM(file_get_contents($sHOME . $win32 . $file)); if ($BOM) { $BOMBED[count($BOMBED)] = $HOME . $win32 . $file; echo '<span class="BOM">'.$HOME . $win32 . $file."</span><br />\n"; } else { echo '<span class="FILE">'.$HOME . $win32 . $file."</span><br />\n"; } } } } $folder->close(); if(count($foundfolders) > 0) { foreach ($foundfolders as $folder) { RecursiveFolder($folder, $win32); } } } function SearchBOM($string) { if(substr($string, 0,3) == pack("CCC",0xef,0xbb,0xbf)) { return true; } return false; } ?> </body> </html> |
Tagged: bom, byte order mark, declaration error, header error, remove bom, rss feed, utf 8, utf8, utf8 bom, xml

