Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 剖析网页字符集的设置顺序

剖析网页字符集的设置顺序

Jun 23, 2016 pm 02:17 PM

字符集

作者:360weboy
新浪微博:http://weibo.com/360weboy
博文链接: http://www.360weboy.com/php/fundament/charset.html

上个星期在为一个电子商务网站进行数据转移到新系统中的时候,发现一些产品描述字符老是输出乱码,确定是字符集的问题以后,对字符集再次进行了一次调研,确定了影响页面字符集的方式主要下面几种方式:
1. 文件的编码方式
2. Apache2的默认字符集设置
3. PHP.ini中的默认字符集的设置
4. PHP脚本中手动输出header(‘Content-type:text/html;charset=xxx’);
5. html页面中加入
我对以上五种方式都进行了一一测试,并且确定了5种方式对于浏览器字符集选择的优先级。首先我创建了一个以utf8编码的test.php文件,内容如下:



我使用chrome访问test.php, 因为我的chrome浏览器的默认字符集不是utf8,应该是gb2312或者gbk,所以我在浏览器中看到了如下乱码:



header与meta的优先级比较

接下来,先来测试以上的4,5两种方式。我向html页面中分别加入了,内容显示正常。





去掉header中的meta标签,加入header(‘Content-type:text/html;charset=utf8′);内容显示正常。那么,meta标签和header的方式,那个优先级高呢,我在页面里同时设置了这两项,header设置为gb2312, meta设置为utf8:



结果内容显示不正常,说明header的优先级高。浏览器先采用http头部中的字符集设置,然后是html页面中meta标签设置的字符集。



php.ini中设置默认字符集以后的影响

接下来,我们来看下在php.ini中设置默认字符集以后的情形。为了测试下优先级,我们先将页面中的header以及meta设置的字符集都改为gb2312,那么这样内容肯定显示为乱码。然后,我们打开php.ini文件,找到如下设置,去掉前面的引号,设置字符集为utf8:



设置好以后,记得要重启下apache2服务器,结果显示如下:



我们看到,在php.ini设置默认字符集为utf8以后,它被加入到了响应头Content-type末尾,从而覆盖了在php脚本中通过header函数输出的gb2312的字符集,浏览器根据该头部中的信息,认为内容是utf8的字符集,最后内容现实正常。由此可见,php.ini中字符集设置的优先级高于header函数以及meta标签。

apache2中设置默认字符集

最后,我们来设置下apache2中的默认字符集来测试下。这次将header,meta,php.ini中的字符集都设置为gb2312,然后在apache2设置默认字符集为utf8:



重启apache2服务器,内容显示如下:





由此可见,apache2中的字符集的设置没有影响到http响应中的Content-Type头部,所以浏览器认为应该用gb2312来解码,导致出现乱码。那么,如果去掉php.ini中的字符集的设置以后内,页面是否会显示正常。经过测试,显示如下:



看来,apache2中的字符集的优先级同样小于header函数的字符集设置。我们继续去掉header的设置看下:

以上证明,apache2中的字符集设置的优先级高于meta标签的字符集设置。charset=utf8加入到了http头部中。
综合上述实验,得出字符集设置优先级顺序: php.ini默认字符集设置 > header函数字符集设置 > apache2默认字符集设置 > meta标签字符集设置

回复讨论(解决方案)

这个结果不太认同

测试有局限性
1.只使用了单一浏览器测试
2.只使用了一种语系测试
3.只使用了单一文件编码测试

最大的问题是所有测试都是正向测试,就是符合预期就认为该项是正确的,这是逻辑大忌

为什么实验过程没有问题,但得出的结论是错误的呢?
经测试:
default_charset 的优先级小于 header 函数字符集设置

优先级的顺序根据我和朋友的测试是对的,请自己核对。

snmr_com

请指教,你知道测试问题所在,那么能不能说下,这个过程应该怎么测试才算标准。。多谢!

http://www.w3help.org/zh-cn/causes/HR9001
可以参考这边文章,但这文章也比较旧了

你用的是归纳法推导,完全归纳法的基础就是满足所有的可能性得出一致(唯一)结论
并且,因为你的这个命题各种可能性并非符合“良序”特性,不能用数学归纳法那样的两步求证
所以,你要做测试就要遍历所有的可能性,如果没有遍历所有可能性,那么结论就只能增加限定条件
例如
只涉及一种浏览器,结论就只能缩小到该浏览器
原文档只使用了一种编码,那结论也只能适用于这种编码

基本上增加了两三个限定条件,结论就没有太大的实用意义,因为适用范围变得很小了
所以还是要做更多的测试以支持你的结论才行
你命题涉及的条件有:浏览器、各优先级的编码指定、语系(特别是较难自动识别的不同语系,如同是双字节中日韩编码)、文档编码,做一个排列组合,可能性就相当多了……如果能全部测试一遍,还是相当佩服你的,呵呵

专业的理论指导,兄弟佩服。。。有时间再完整的走一遍。。多谢回复

优先级的顺序根据我和朋友的测试是对的,请自己核对。 php.ini



运行界面


你测试的结果并不代表我测试的结果
何况 header 就该能改变相关预设值,否则还要他干什么?

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles