Default encoding problem resolution for Apache and PHP (detailed)

Source: Internet
Author: User
Tags script php blank page

If you search for "Apache configuration" on the Internet, most of the pages searched will suggest that you add such a sentence in httpd.conf: Adddefaultcharset GB2312. This is OK for novices and developers who are only using GB2312 coding. However, if you want to use the UTF-8 character set, such as in the test.php file need to have meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "This piece of code. Then you open the browser to access the test.php page, you see the correct page. But what if the browser actually interprets the response returned from the server with GB2312 encoding? The reason is that the browser responds to the content-type:text/html in the header of the message based on HTTP; charset=gb2312 to decide which encoding to use to interpret the answer, which means that the Apache server still passes the data with GB2312 encoding. So if Apache's default charset is set to GB2312, even if the UTF-8 encoding is declared on the page, the Apache server will send the HTTP response according to GB2312 encoding. It doesn't matter, we change adddefaultcharset GB2312 to Adddefaultcharset UTF-8, see what result? If you see garbled to congratulate you, you also know is garbled problem, if you see a blank page, then you are miserable, you may think this is what other causes, and not from the point of view of coding to consider how to solve the problem. Why is this? The reason is that the PHP file itself is encoded using the system character set, Chinese Windows XP is GB2312, each file header has a field to indicate how the file is encoded. When Apache receives a request from the browser, it will allow PHP to explain the requested page, such as test.php. PHP will recognize that the encoding of test.php is GB2312 (as we compile Java source files with Javac, the compiler reads the contents of the source file by default in the system code.) If the source file is not saved with the system code, you can specify the specific encoding with the command javac-encoding, and the data is passed to Apache in GB2312 encoding format, and the Apache server will not change the data from PHP. Just set the character set to UTF-8 in the header of the reply message: content-type:text/html; Charset=utf-8. Which means you're passing GB2312 encoded data,The browser interprets the reply message with UTF-8 encoding. Since UTF-8 represents a man for 3 bytes, the normal GB2312 or BIG5 is two. Page output, because of the above reasons, the occurrence of half a Chinese character, then the half of the Chinese character will be combined with the > into a garbled character, resulting in IE can not read the words, will find that in fact the entire leaf has been output. If you use Mozilla, Mozilla Firefox, Sarafi Browser This will not cause this problem, but a bunch of garbled. This is due to the Firefox browser and IE parsing the page encoding strategy of different generation. OK, when we save the test.php in UTF-8 and then use the browser to access it, there is no problem. Doing so allows all web apps in the Apache directory to be encoded in the same way. How to fix it? Workaround: First, you can use adddefaultcharset off to turn off the default file encoding so that the Apache server does not set charset on the HTTP reply message header, just set the content-type:text/html. The browser relies on the Harset set in the HTML file to determine the encoding. Second, the script php.ini file Default_charset = "Utf-8″ function with the httpd.conf file, the line commented out, so that PHP automatically recognize the encoding of the file. So no matter what encoding you use, as long as the meta http-equiv= in test.php "Content-type" content= "text/html; Charset=utf-8″ and your test.php file encoding the same way, there will be no garbled problem.

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.