Root Cause of page garbled characters

Source: Internet
Author: User

This problem has been encountered twice, that is, the page clearly indicates that the encoding is UTF8, but the display is garbled.

Although I know the solution and the reason for Apache, I haven't tried to find it. Today I took the opportunity to study it.

First, disable default_charset in the PHP configuration file php. 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:

 
 
  1. 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.

 
 
  1. <Head>
  2. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  3. </Head>
  4. <Body>
  5. <Div id = "page-header">
  6. Test Apache DefaultCharset
  7. </Div>
  8. </Body>
  9. </Html>

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

 
 
  1. 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:

 
 
  1. Adddefacharcharset command
  2. Indicates the default Character Set added to the HTTP Response Header when the response content is text/plain or text/html.
  3. Syntax adddefacharcharseton | Off | charset
  4. Default Value: adddefacharcharsetoff
  5. Scope: serverconfig, virtualhost, directory,. htaccess
  6. Overwrite FileInfo
  7. Status core (C)
  8. Module core
  9. This command is added to the HTTP Response Header only when the response content is text/plain or text/html.
  10. Default character set. Theoretically, this will overwrite the character set specified by the <meta> tag in the document body, but the actual
  11. Usually depends on the user's browser settings. Adddefacharcharsetoff will disable this function.
  12. Adddefacharcharseton enables the default Character Set iso-8859-1 within Apache. You
  13. You can also specify another charset in the character set name registered in IANA.
  14. For example:
  15. 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.

Address: http://www.laruence.com/2008/04/17/110.html

 

 

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.