【问题标题】:Character encoding not declared in html document未在 html 文档中声明的字符编码
【发布时间】:2012-07-22 21:45:34
【问题描述】:

我有一个文件,我收到一个非常奇怪的错误。错误是:

The character encoding of the HTML document was not declared. 
The document will render with garbled text in some browser configurations if 
the document contains characters from outside the US-ASCII range. 
The character encoding of the page must to be declared in the document or 
in the transfer protocol.

这个来自的文件是(indexmws.php):

session_start();
if(!isset($_SESSION['validUser']) || $_SESSION['validUser'] !== true){
header('Location: loginmws.php');
}

include_once('db.php');
include_once('amazonmws.php');
include_once('decidemws.php');
include_once('author.php');
include_once('amazonPricingMWS.php');

?>

<!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" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Decision Maker</title>

除了添加 amazonPricingMWS.php 并重定向到标题中包含 mws.php 的页面之外,这是一个不会引发错误的文件 (index.php) 的完全相同的副本:

session_start();
if(!isset($_SESSION['validUser']) || $_SESSION['validUser'] !== true){
header('Location: login.php');
}

include_once('db.php');
include_once('amazon.php');
include_once('decide.php');
include_once('author.php');

?>

<!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" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Decision Maker</title>

谁能向我解释为什么我在 indexmws.php 中出现此错误?

【问题讨论】:

    标签: php encoding


    【解决方案1】:

    出现错误是因为浏览器期望文件的前 1024 个字节中的编码格式。可能是第一种情况下被包含的文件输出了一些内容。

    浏览器现在缓冲文件的前 1024 个字节以检查字符编码。如果在前 1024 个字节中没有遇到编码描述,则会显示此警告。

    在您的情况下,您可以在包含其他文件之前使用 php 标头来指定内容类型:

    header('Content-type: text/html; charset=utf-8');
    

    有关更多信息,请阅读以下内容: http://gtmetrix.com/specify-a-character-set-early.html

    【讨论】:

      【解决方案2】:

      我也有类似的问题,但这背后的确切原因是我错过了添加

      <meta content="utf-8" http-equiv="encoding">
      

      在头标签之后

      <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
      

      【讨论】:

      • 谢谢!这解决了我在 Firefox 中的错误,(我有 ,但它是需要元标记的 HTML。)我在 之后使用了你的第一个,(虽然我把它的结尾改成了 '" />' 以匹配页面上的标签样式。)
      • @Krishan,你在哪个浏览器上测试过这个?
      • 我刚试过&lt;meta charset="utf-8"/&gt;,它也有效
      猜你喜欢
      • 2015-12-05
      • 2014-09-12
      • 1970-01-01
      • 2012-11-09
      • 2014-08-27
      相关资源
      最近更新 更多