Recently, students often encounter such coding errors when using Lamp/wamp:
A website program encoding UTF-8 code installed successfully, running successfully.
B website program gb2312 also installed on the same server.
This is a problem, the Apache default encoding UTF-8 in the resolution of a site when there is no problem, when running B site when the "Shingwen" garbled problem.
Simply modify the Apache default encoding for gb2312 This causes a website to appear "Shingwen".
Problem Analysis:
If you search "Apache configuration" on the Internet, most of the pages you search will suggest that you add a httpd.conf: Adddefaultcharset GB2312.
For beginners and developers who only use GB2312 coding, this is OK. However, 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 "This piece of code.
When you open the browser to visit the test.php page again, you see the correct page. But what if in fact the browser still interprets the response returned from the server in GB2312 encoding? The reason is that the browser is based on the HTTP reply message in the head of the content-type:text/html; charset=gb2312 to decide which encoding to use to interpret the answer, which means that the Apache server still transmits data using GB2312 encoding.
So if Apache's default character set 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 the GB2312 code. It doesn't matter, we change adddefaultcharset GB2312 to Adddefaultcharset UTF-8, see what result?
If you see the garbled code congratulations, you also know is garbled problem; If you see a blank page, then you are miserable, you may think that this is caused by other reasons, and not from the point of view of coding to solve the problem. Why is that? The reason is that the PHP file itself is encoded with the system character set, and Windows XP in Chinese is GB2312, and each file header has a field indicating how the file is encoded. When Apache receives a request from the browser, PHP is asked to explain the requested page, such as test.php. PHP will recognize that test.php encoding is GB2312 (just as we use Javac to compile Java source files, the compiler defaults to reading the contents of the source file with system encoding.
If the source file is not saved with system code, you can specify the specific encoding with the command javac-encoding, and the data is passed to Apache in a GB2312 encoded format, and the Apache server does not change the data from PHP, Just set the character set to UTF-8 in the reply message head: content-type:text/html; Charset=utf-8. That means you're passing GB2312 encoded data, and the browser interprets the answer message in UTF-8 code.
Since UTF-8 represents a man for 3 bytes, the average GB2312 or BIG5 is two. Page output, due to the above reasons, the situation of half Chinese characters, then the half of the Chinese characters will be combined with the > into a garbled word, resulting in IE can not read the words, will find that in fact the entire leaf has been exported. If the use of Mozilla, Mozilla Firefox, Sarafi Browser This will not cause this problem, but a heap of garbled. This is due to the Firefox browser and IE parsing page encoding strategy is different. OK, we put the test.php to UTF-8 to save, then use the browser to visit, there is no problem.
This allows all Web applications in the Apache directory to be encoded only in the same way. How do you handle it?
Solution:
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 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 httpd.conf file, comment out the line, so that PHP automatically recognize the encoding of the file."
So no matter what encoding you use, as long as the test.php in the meta http-equiv= "Content-type" content= "text/html;" Charset=utf-8″ and you test.php file encoding method is the same, will not produce garbled problem. The user submits the data to the encoding browser to submit the character encoding determined by the client's Characher encoding.
For example, the current browser's encoding is Gb2312, after the user submits the data, regardless of the Apache settings encoding is GB2312 or UTF-8, then the server received is still the Gb2312 encoded data.
If you want to display the data that the user has just submitted on the return page, which is UTF-8 encoded or submitted by the user to be stored in the database, and the database is UTF-8 encoded, then character conversion is necessary.
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.