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:
- 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.
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
- </Head>
- <Body>
- <Div id = "page-header">
- Test Apache DefaultCharset
- </Div>
- </Body>
- </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
- 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 overwrite the character set specified by the <meta> tag in the document body, 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.
Address: http://www.laruence.com/2008/04/17/110.html