php 未在 html 文档中声明的字符编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11633162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Character encoding not declared in html document
提问by Jim
I have a file that I am getting a very weird error on. The error is:
我有一个文件,我收到一个非常奇怪的错误。错误是:
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.
the file this comes from is (indexmws.php):
这来自的文件是(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>
This is an exact duplicate of a file that does not throw the error (index.php)with the exception of adding amazonPricingMWS.php and redirecting to pages with mws.php in the title:
这是一个不会抛出错误 (index.php) 的文件的完全副本,除了添加 amazonPricingMWS.php 并重定向到标题中带有 mws.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>
can anyone explain to me why I am getting this error in indexmws.php?
谁能向我解释为什么我在 indexmws.php 中收到此错误?
回答by Karan Punamiya
The error is coming because the browsers expect the encoding format in the first 1024 bytes of the file. It may be the case that there is some content being outputted by the included files in the first case.
错误即将到来,因为浏览器期望文件的前 1024 个字节中的编码格式。可能是第一种情况下包含文件输出了一些内容。
The browsers now buffer the first 1024 bytes of the file to check for the character encoding. If the encoding description is not encountered in the first 1024 bytes, this warning is displayed.
浏览器现在缓冲文件的前 1024 个字节以检查字符编码。如果在前 1024 个字节中未遇到编码描述,则会显示此警告。
In your case, you can use a php header for specifying the content type before the other files are included:
在您的情况下,您可以使用 php 标头在包含其他文件之前指定内容类型:
header('Content-type: text/html; charset=utf-8');
For more information, read this: http://gtmetrix.com/specify-a-character-set-early.html
有关更多信息,请阅读:http: //gtmetrix.com/specify-a-character-set-early.html
回答by Krishan Gopal
I had similar problem But teh exact reason behind this was that I missed following to add
我有类似的问题但是这背后的确切原因是我错过了以下添加
<meta content="utf-8" http-equiv="encoding">
in head tag after
在头标签之后
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">

