Solutions to Apache default encoding errors and website garbled characters

Source: Internet
Author: User
Tags script php blank page

Recently, some people often encounter such encoding errors when using lamp/Wamp:

Website a program code UTF-8 code installation successful, run successful.

B website program gb2312 must also be installed on the same server.

So there is a problem, Apache default encoding UTF-8 in the resolution of a site without any problem, when running B site when the "zookeeper text" garbled problem.

Simply modifying the default Apache code to gb2312 causes a website a to see the "invalid answer ".

Problem Analysis:

If you search for "Apache configuration" on the Internet, we recommend that you add the following sentence to httpd. conf: adddefacharcharset gb2312.

This is OK for beginners who only use gb2312 encoding. But if you want to use the UTF-8 character set, for example, in the test. php file, you need meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8" code.

In this case, if you open your browser to access the test. PHP page, you will see the correct page. But if the browser still uses gb2312 encoding to explain the response returned from the server, why? The reason is that the browser determines the encoding used to interpret the response based on the Content-Type: text/html; charset = gb2312 in the HTTP Response Message Header. That is to say, the Apache server still uses gb2312 encoding to transmit data.

So if Apache's default character set is set to gb2312, even if it is declared to use UTF-8 encoding on the page, the Apache server still transmits HTTP response according to gb2312 encoding. It doesn't matter. Let's change adddefaultcharset gb2312 to adddefacharcharset UTF-8 to see what results?

If you see Garbled text, congratulations, you still know it's a garbled text problem; if you see a blank page, then you're miserable, you may think this is caused by other reasons, instead, we will not consider how to solve the problem from the encoding perspective. Why? The reason is that PHP files are encoded using the system character set. Windows XP in Chinese uses gb2312. Each file header has a field to indicate the encoding method of the file. When apache receives a request from the browser, it will ask PHP to explain the requested page, such as test. php. PHP will recognize that the encoding method of test. php is gb2312 (just like when we use javac to compile the Java source file, the compiler uses the system encoding by default to read the content in the source file.

If the source file is not saved using system encoding, you can use the javac-encoding command to specify the specific encoding.) The data is transmitted to Apache in gb2312 encoding format, the Apache server does not change the data from PHP, but sets the character set to UTF-8: Content-Type: text/html; charset = UTF-8 in the response message header. that is to say, you pass the gb2312 encoding data, but the browser is coded in UTF-8 to explain the Response Message.

Because the UTF-8 is 3 bytes to indicate a man, while the ordinary gb2312 or big5 is two. When the page is output, half a Chinese character may occur due to the above reasons. Then, the half Chinese character will be combined with the & gt; into a garbled character, which causes IE to fail to read the text, you will find that all the leaves have been output. If you are using Mozilla, Mozilla Firefox, and sarafi browsers, this will not cause this problem, but a bunch of garbled characters. This is because the Firefox browser and IE parse webpage encoding policies are different. OK, we save test. php as a UTF-8, and then access the browser, there is no problem.

In this way, all web applications under the Apache directory can only use the same encoding. How can this problem be solved?

Solution:

First, you can use adddefacharcharset off to disable the default file encoding, so that the Apache server does not set charset in the HTTP Response Message Header, but only sets Content-Type: text/html. the browser depends on the harset set in the HTML file to determine the encoding.

Second, the default_charset = "UTF-8" in the script PHP. ini file works with the httpd. conf file, comment out the line, so that PHP automatically recognizes the file encoding method.

In this way, no matter what encoding method you use, you only need test. meta in PHP http-equiv = "Content-Type" content = "text/html; charset = UTF-8" with you test. PHP files are encoded in the same way. The character encoding submitted by the browser is determined by the characher encoding of the client.

For example, the current browser encoding is gb2312, after the user submits the data, whether Apache sets the encoding method is gb2312 or UTF-8, then the server side received the data is still in gb2312 encoding.

If you want to display the data that the user just submitted on the return page, which is UTF-8-encoded or the data that the user submitted to store in the database, and the database is UTF-8-encoded, character conversion is required.

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.