Home  >  Article  >  Backend Development  >  The root cause of the garbled page problem and its analysis_PHP tutorial

The root cause of the garbled page problem and its analysis_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:59:48800browse

First, turn off the default_charset in the PHP configuration file php.ini:
1. The page does not specify charset, Apache configures defaultcharst gbk, and the page file encoding is utf-8
Result: Garbled code, use wireshark to capture the packet, and find that the header returned by the server specifies:

Copy the code The code is as follows:

Content-Type: text/html;charset=GB

Conclusion: When the page does not specify charset, Apache’s defaultcharset works

2. The specified charset of the page is utf-8, and Apache configures defaultcharset gbk. The page file is utf-8

Copy code The code is as follows:


    


🎜>


The result is still garbled characters.
Conclusion: When Apache is configured with DefaultCharset, the charset declaration of the page will be ignored.

3. PHP header declares charset as utf8, Apache configures defaultcharst gbk, and page file encoding is utf8


Copy code

The code is as follows :header("Content-Type: text/html; charset=utf-8");
Result: The page displays normally.

4 Apache sets DefaultCharset off
As a result, the page displays normally.


Read the Apache2 manual:

AddDefaultCharset directive explains that when the response content is text/plain or text/html, add it to the HTTP response header Default Charset
Syntax AddDefaultCharsetOn|Off|charset
Default AddDefaultCharsetOff
Scope serverconfig, virtualhost,directory,.htaccess
Override FileInfo
State Core (C)
Module core
If and only if the response content is text/plain or text/html, this directive will add the
default character set in the HTTP response header. Theoretically this will override the character set specified via the tag in the document body, but the actual behavior of
usually depends on the user's browser settings. AddDefaultCharsetOff will disable this feature.
AddDefaultCharsetOn will enable the default character set within Apache, iso-8859-1. You
can also specify to use another charset from one of the character set names registered with IANA.

For example:

AddDefaultCharsetutf-8
In other words, when Apache does not specify defaultcharset, the page encoding is specified by the page's own meta tag.
When specified by Apache, the encoding specified by the meta tag in the page will be ignored. However, the script is allowed to directly encode the header to the client

Finally, there is another question that has not yet been resolved:

What if neither Apache nor the page are specified?
I am on my own machine. If I don’t specify anything, the default is still utf8.



http://www.bkjia.com/PHPjc/328117.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/328117.htmlTechArticleFirst, close the default_charset in the PHP configuration file php.ini: 1. The page does not specify charset, Apache configures defaultcharst gbk , the page file encoding is utf-8. Result: garbled code, use wiresh...
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