Solution for Apache default encoding error and website garbled code, apache encoding _ PHP Tutorial

Source: Internet
Author: User
Tags script php
Apache encoding is a solution that causes website garbled characters due to a default apache encoding error. About the solution to Apache default encoding errors that cause website garbled characters, apache encoding has recently encountered such encoding errors when using LAMPWAMP: website A program code: the default Apache code is incorrect, which causes the website to be garbled.

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.

When using LAMP/WAMP, users often encounter such encoding errors: website A program code...

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.