首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >未在html文档中声明的字符编码

未在html文档中声明的字符编码
EN

Stack Overflow用户
提问于 2012-07-24 22:33:49
回答 2查看 60.5K关注 0票数 16

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

代码语言:javascript
复制
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):

代码语言:javascript
复制
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>

这是一个不抛出错误(index.php)的文件的完全副本,只是添加了amazonPricingMWS.php并重定向到标题中包含mws.php的页面:

代码语言:javascript
复制
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中遇到这个错误吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-24 23:25:10

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

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

在您的示例中,您可以在包含其他文件之前使用php头指定内容类型:

代码语言:javascript
复制
header('Content-type: text/html; charset=utf-8');

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

票数 24
EN

Stack Overflow用户

发布于 2012-10-10 21:02:21

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

代码语言:javascript
复制
<meta content="utf-8" http-equiv="encoding">

在head标记后

代码语言:javascript
复制
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11633162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档