Root cause and analysis of page garbled text _ PHP Tutorial

Source: Internet
Author: User
Root cause and analysis of page garbled text. First, close the PHP configuration file php. default_charset in ini: 1. charset is not specified on the page, defaultcharstgbk is configured in Apache, and the page file encoding is UTF-8 result: Garbled. use wiresh to first close the PHP configuration file php. default_charset in ini:
1. charset is not specified on the page. Apache configures defaultcharst gbk and the page file encoding is UTF-8.
Result: garbled characters are captured using wireshark. the header returned by the server indicates:

The code is as follows:


Content-Type: text/html; charset = GB


Conclusion: Apache defaultcharset takes effect when charset is not specified on the page.

2. set charset to UTF-8 on the page, and configure defaultcharset gbk in Apache. the page file is UTF-8.

The code is as follows:







Test Apache DefaultCharset





The result is garbled.
Conclusion: when DefaultCharset is configured for Apache, the charset statement of the page is ignored.

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

The code is as follows:


Header ("Content-Type: text/html; charset = utf-8 ");


Result: The page is displayed normally.

4. set DefaultCharset off in Apache
Result: The page is displayed normally.

Read the Apache2 manual:
Adddefacharcharset command
Indicates the default character set added to the HTTP response header when the response content is text/plain or text/html.
Syntax adddefacharcharseton | Off | charset
Default value: adddefacharcharsetoff
Scope: serverconfig, virtualhost, directory,. htaccess
Overwrite FileInfo
Status core (C)
Module core
This command is added to the HTTP response header only when the response content is text/plain or text/html.
Default character set. Theoretically, this will cover The character set specified by the tag, but the actual
Usually depends on the user's browser settings. Adddefacharcharsetoff will disable this function.
Adddefacharcharseton enables the default character set ISO-8859-1 within Apache. You
You can also specify another charset in the character set name registered in IANA.
For example:
AddDefaultCharsetutf-8

That is, when defaultcharset is not specified for Apache, the page encoding is specified by the meta tag of the page.
When Apache is specified, the encoding specified by the meta tag on the page is ignored, but the script can be directly used as the header encoding method to the client.

Finally, there is another problem that has not been returned:
What if Apache and the page are not specified?
If none of my machines are specified, the default value is utf8.

Disable: disable default_charset: 1 in PHP configuration file php. ini. the charset is not specified on the page. Apache configures defacharcharst gbk. the page file is encoded as UTF-8 and garbled. wiresh is used...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.