HTML中head裡的內容經瀏覽器解析後全到body裡

來源:互聯網
上載者:User

標籤:

我從linux伺服器nginx上把一個網站遷移到windows的IIS上
資料什麼的都麼有問題,配置好rewrite以後,訪問網站,發現樣式變動了,網站上方空出了一塊
我用chrome瀏覽器的審查元素一看,發現head裡的內容全到Body裡了,而且body的最開始出多出了一塊帶引號的空白!

但是如果右鍵查看原始碼的話,代碼是正確的,沒有問題!


正常狀態


錯誤狀態

 

 

是include的問題,包含的檔案是utf-8編碼的,但是有BOM頭,我用一個函數把網站目錄裡的所有檔案的BOM頭去掉以後就恢複正常了,希望看到文章的同志們以後遇到這樣的問題不要跟我一樣糾結了,順便貼一下我去除BOM頭的函數代碼,希望大家能用上,不要像我一樣糾結了

 

<?php $basedir = str_replace(‘/clearBOM.php‘,‘‘,str_replace(‘\\‘,‘/‘,dirname(__FILE__)));$auto = 1;checkdir($basedir);function checkdir($basedir){    if ($dh = opendir($basedir)) {        while (($file = readdir($dh)) !== false) {            if ($file != ‘.‘ && $file != ‘..‘){                if (!is_dir($basedir.‘/‘.$file)) {                    $filename = $basedir.‘/‘.$file;                    echo ‘filename:‘.$basedir.‘/‘.$file.checkBOM($filename).‘<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);}?>

 

使用的方法是將該代碼放入檔案後放在網站的根目錄,或者你想去哪個目錄的BOM就放在哪個目錄,然後直接用URL訪問這個PHP檔案就可以了!

 

HTML中head裡的內容經瀏覽器解析後全到body裡

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.