如何使用在 W3C 验证器中有效的 PHP 将 HTTP 标头设置为 UTF-8?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4279282/
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
How to set HTTP header to UTF-8 using PHP which is valid in W3C validator?
提问by manycheese
I have several PHPpages echoing out various things into HTMLpages with the following code.
我有几个PHP页面使用以下代码将各种内容回显到HTML页面中。
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
However, when I validate using the W3C validatorit comes up with:
但是,当我使用W3C 验证器进行验证时,它会出现:
The character encoding specified in the HTTP header (iso-8859-1) is different from the value in the element (utf-8).
HTTP 标头 (iso-8859-1) 中指定的字符编码与元素中的值 (utf-8) 不同。
I am quite new to PHP, and I was wondering if I could and should change the header for the PHP files to match the HTML files.
我对 PHP 很陌生,我想知道我是否可以并且应该更改 PHP 文件的标头以匹配 HTML 文件。
回答by Gumbo
Use header
to modify the HTTP header:
使用header
修改HTTP标头:
header('Content-Type: text/html; charset=utf-8');
Note to call this function before any output has been sent to the client. Otherwise the header has been sent too and you obviously can't change it any more. You can check that with headers_sent
. See the manual page of header
for more information.
请注意在将任何输出发送到客户端之前调用此函数。否则,标头也已发送,您显然不能再更改它了。您可以使用headers_sent
. 有关更多信息,请参阅 的手册页header
。
回答by KingCrunch
First make sure the PHP files themselves are UTF-8encoded.
首先确保 PHP 文件本身是UTF-8编码的。
The meta tag is ignored by some browser. If you only use ASCII-characters, it doesn't matter anyway.
某些浏览器会忽略元标记。如果您只使用 ASCII 字符,那也没关系。
http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
header('Content-Type: text/html; charset=utf-8');
回答by EdoDodo
This is a problem with your web server sending out an HTTP header that does not match the one you define. For instructions on how to make the server send the correct headers, see this page.
这是您的 Web 服务器发送的 HTTP 标头与您定义的标头不匹配的问题。有关如何使服务器发送正确标头的说明,请参阅此页面。
Otherwise, you can also use PHP to modify the headers, but this has to be done beforeoutputting any text using this code:
否则,您也可以使用 PHP 修改标题,但这必须在使用以下代码输出任何文本之前完成:
header('Content-Type: text/html; charset=utf-8');
More information on how to send out headers using PHP can be found in the documentation for the header function.
有关如何使用 PHP 发送标头的更多信息,请参见标头函数的文档。
回答by Jason OOO
回答by UnChien Andalou
For a correct implementation, you need to change a series of things.
为了正确的实现,你需要改变一系列的东西。
Database (immediately after the connection):
数据库(连接后立即):
mysql_query("SET NAMES utf8");
// Meta tag HTML (probably it's already set):
meta charset="utf-8"
header php (before any output of the HTML):
header('Content-Type: text/html; charset=utf-8')
table-rows-charset (for each row):
utf8_unicode_ci
回答by Nikl
PHP sends headers automatically if set up to use internal encoding:
如果设置为使用内部编码,PHP 会自动发送标头:
ini_set('default_charset', 'utf-8');