Home  >  Article  >  Backend Development  >  Detailed explanation of PHP page encoding declaration method (header or meta)

Detailed explanation of PHP page encoding declaration method (header or meta)

不言
不言Original
2018-06-06 11:45:042475browse

This article mainly introduces the detailed explanation of the PHP page encoding declaration method (header or meta), which has a certain reference value. Now I share it with you. Friends in need can refer to it

PHP page encoding declaration The difference between using header or meta to implement PHP page encoding

php header defines a php page as utf encoding or GBK encoding

php page is utf encoding

header("Content-type: text/html; charset=utf-8");

php page is gbk encoded

header("Content-type: text/html; charset=gb2312");

php page is big5 encoded

header("Content-type: text/html; charset=big5");

通常情况以上代码放在php页面的首页
用header或meta实现PHP页面编码的区别
一、页面编码
1. 使用 dfd211f9c4fa5f15856b8f413df08033 标签设置页面编码
这个标签的作用是声明客户端的浏览器用什么字符集编码显示该页面,xxx可以为GB2312,GBK,UTF-8(和MySQL不同,MySQL是 UTF8)等等。因此,大部分页面可以采用这种方式来告诉浏览器显示这个页面的时候采用什么编码,这样才不会造成编码错误而产生乱码。但是有的时候我们会 发现有了这句还是不行,不管xxx是哪一种,浏览器采用的始终都是一种编码,这个情况我后面会谈到。
请注意,e8e496c15ba93d81f6ea4fe5f55a2244是属于html信息的,仅仅是一个声明,它起作用表明服务器已经把HTML信息传到了浏览器。
2. header("content-type:text/html; charset=xxx");
这个函数header()的作用是把括号里面的信息发到http标头。
如果括号里面的内容为文中所说那样,那作用和3f2ae36b77c0b1762e465ca019e7bd30标签基本相同,大家对照第一个看发现字符都差不多的。但是不同的是如果有这段 函数,浏览器就会永远采用你所要求的xxx编码,绝对不会不听话,因此这个函数是很有用的。为什么会这样呢?那就得说说HTTPS标头和HTML信息的差 别了:
https标头是服务器以HTTP协议传送HTML信息到浏览器前所送出的字串。
因为meta标签是属于html信息的,所以header()发送的内容先到达浏览器,通俗点就是header()的优先级高于meta(不知道可 不可以这样讲)。加入一个php页面既有header("content-type:text/html; charset=xxx"),又有dfd211f9c4fa5f15856b8f413df08033,浏览器就只认前者http标头而不认meta了。当然这个函数只能在php页面内使用。
同样也留有一个问题,为什么前者就绝对起作用,而后者有时候就不行呢?这就是接下来要谈的Apache的原因了。
3. AddDefaultCharset
Apache 根目录的 conf 文件夹里,有整个Apache的配置文档httpd.conf。
用文本编辑器打开httpd.conf,第708行(不同版本可能不同)有AddDefaultCharset xxx,xxx为编码名称。这行代码的意思:设置整个服务器内的网页文件https标头里的字符集为你默认的xxx字符集。有这行,就相当于给每个文件都 加了一行header("content-type:text/html; charset=xxx")。这下就明白为什么明明meta设置了是utf-8,可浏览器始终采用gb2312的原因。
如果网页里有header("content-type:text/html; charset=xxx"),就把默认的字符集改为你设置的字符集,所以这个函数永远有用。如果把AddDefaultCharset xxx前面加个“#”,注释掉这句,而且页面里不含header("content-type…"),那这个时候就轮到meta标签起作用了。
总结:
来个排序
header("content-type:text/html; charset=xxx")
AddDefaultCharset xxx
dfd211f9c4fa5f15856b8f413df08033
如果你是web程序员,给你的每个页面都加个header("content-type:text/html; charset=xxx"),保证它在任何服务器都能正确显示,可移植性强。
至于那句AddDefaultCharset xxx,要不要注释就仁者见仁了。反正我是注释掉了,不过我写页子也要写header(),便于放到服务器上能正常显示。
二、数据库编码
PHP 程序在查询数据库之前,首先执行 mysql_query(“SET NAMES xxxx”);其中 xxxx 是你网页的编码(charset=xxxx),如果网页中 charset=utf8,则 xxxx=utf8,如果网页中 charset=gb2312,则xxxx=gb2312,几乎所有WEB程序,都有一段连接数据库的公共代码,放在一个文件里,在这文件里,加入 mysql_query(“set names”)就可以了。
SET NAMES 显示客户端发送的 SQL 语句中使用什么字符集。因此,SET NAMES 'utf-8'语句告诉服务器“将来从这个客户端传来的信息采用字符集utf-8”。它还为服务器发送回客户端的结果指定了字符集。(例如,如果你使用一 个SELECT语句,它表示列值使用了什么字符集。)
PHP页面编码统一
MySQL数据库编码、html页面编码、PHP或html文件本身编码要全部一致。
  1、MySQL数据库编码:建立数据库时指定编码(如gbk_chinese_ci),建立数据表、建立字段、插入数据时不要指定编码,会自动继承数据库的编码。
There is also encoding when connecting to the database. After connecting to the database, execute
mysql_query('SET NAMES gbk');//Replace gbk with your encoding, such as utf8.
 2. The encoding of the html page refers to the setting of this line:
ac2713a2bfe4dc6ca85e502d817358c6
3. The encoding of the PHP or html file itself: Use editplus to open the php file or html file. When saving, select the encoding. If the database and page encoding is gbk, then the encoding here selects ansi; if the database and page encoding is utf-8 , then utf-8 is also selected here.
 4. Another thing to note is that the data passed in Javascript or Flash is encoded in utf-8. If the database and page encoding is gbk, it must be transcoded and then written to the database.
iconv('utf-8', 'gbk', $content);
5. In the PHP program, you can add a line to specify the encoding of the PHP source program:
header('Content- type: text/html; charset=gbk');

##

The above is the detailed content of Detailed explanation of PHP page encoding declaration method (header or meta). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn