使用PHP将HTTP头设置为UTF-8。

时间:2022-10-24 23:49:19

I have several PHP pages echoing out various things into HTML pages with the following code.

我有几个PHP页面,用下面的代码将各种内容回显到HTML页面中。

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

However, when I validate using the W3C validator it 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文件。

6 个解决方案

#1


813  

Use header to modify the HTTP header:

使用header修改HTTP header:

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的手册页。

#2


29  

First make sure the PHP files themselves are UTF-8 encoded.

首先确保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');

#3


14  

This is a problem with your web server sending out a 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 before outputting 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发送头信息的更多信息可以在header函数的文档中找到。

#4


10  

You can also use shorter way:

你也可以用更短的方法:

<?php header('Content-Type: charset=utf-8'); ?>

See RFC 2616 its valid to specify only charset.

参见RFC 2616,它的有效性仅指定字符集。

#5


7  

For correct implementation, you need to change a series of things.

为了实现正确,您需要更改一系列内容。

Database.(immediately after the connection):

数据库。(连接后):

mysql_query("SET NAMES utf8");
//Meta tag HTML.(probably its 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

#6


2  

PHP send headers automatically if set up internal encoding

如果设置了内部编码,PHP将自动发送报头

ini_set('default_charset', 'utf-8');

#1


813  

Use header to modify the HTTP header:

使用header修改HTTP header:

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的手册页。

#2


29  

First make sure the PHP files themselves are UTF-8 encoded.

首先确保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');

#3


14  

This is a problem with your web server sending out a 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 before outputting 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发送头信息的更多信息可以在header函数的文档中找到。

#4


10  

You can also use shorter way:

你也可以用更短的方法:

<?php header('Content-Type: charset=utf-8'); ?>

See RFC 2616 its valid to specify only charset.

参见RFC 2616,它的有效性仅指定字符集。

#5


7  

For correct implementation, you need to change a series of things.

为了实现正确,您需要更改一系列内容。

Database.(immediately after the connection):

数据库。(连接后):

mysql_query("SET NAMES utf8");
//Meta tag HTML.(probably its 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

#6


2  

PHP send headers automatically if set up internal encoding

如果设置了内部编码,PHP将自动发送报头

ini_set('default_charset', 'utf-8');