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

Jim*_*Jim 16 php encoding

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

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.
Run Code Online (Sandbox Code Playgroud)

它来自的文件是(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>
Run Code Online (Sandbox Code Playgroud)

这是一个不会抛出错误的文件(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>
Run Code Online (Sandbox Code Playgroud)

任何人都可以向我解释为什么我在indexmws.php中收到此错误?

Kar*_*iya 24

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

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

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

header('Content-type: text/html; charset=utf-8');
Run Code Online (Sandbox Code Playgroud)

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


Kri*_*pal 10

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

<meta content="utf-8" http-equiv="encoding">
Run Code Online (Sandbox Code Playgroud)

在标题之后

<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
Run Code Online (Sandbox Code Playgroud)