PHP applies the garbage problem solution set

Source: Internet
Author: User
Tags character set header ini mysql web services root directory

1 Use the label to set the page encoding

The role of this label is to declare the client's browser with what character set code to display the page, XXX can be GB2312, GBK, UTF-8 (and MySQL is different, MySQL is UTF8) and so on. Therefore, most of the pages can be used in this way to tell the browser to display the page when the code, so that will not cause coding errors generated garbled. But sometimes we will find that there is no, no matter what XXX is, the browser is always a kind of coding, which I will talk about later.

Note that it is HTML information, just a declaration, that the server has uploaded HTML information to the browser.

2 Header ("content-type:text/html; Charset=xxx ");

The function header () is to send the information inside the parentheses to the HTTP header. If the contents of the parentheses in the text, that the function and the label is basically the same, we compare the first to see the characters are similar. But the difference is that if you have this function, the browser will always use your request for the XXX code, will not be disobedient, so this function is very useful. Why does it have to be like that? The difference between HTTP headers and HTML information is:

HTTP headers are strings sent by the server before sending HTML information to the browser with the HTTP protocol. And the label is HTML information, so header () to send the content to reach the browser, popular point is header () priority is higher (do not know can say so). If a PHP page has both header ("Content-type:text/html;charset=xxx"), and then, the browser will only recognize the former HTTP headers and not to recognize Meta. Of course, this function can only be used within the PHP page.

There is also a question, why is the former absolutely effective, and the latter sometimes not? This is the reason for the next point about Apache.

3) Adddefaultcharset

In the Conf folder of the Apache root directory, there is the entire Apache configuration document HTTPD.CONF.

Open httpd.conf with a text editor, line No. 708 (different versions may be different) has adddefaultcharset xxx,xxx as the encoded name. This line of code means: Set the entire server within the page file HTTP header character set for your default XXX character set. With this line, it is equivalent to adding a line header to each file ("content-type:text/html; Charset=xxx "). This is clear why clearly set is Utf-8, can always use the browser gb2312 reasons.

If the page has header ("content-type:text/html; Charset=xxx "), the default character set is changed to the character set of your setting, so this function is always useful. If you put a "#" in front of adddefaultcharset xxx, comment out the sentence, and the page does not contain header ("Content-type ..."), then the META tag will work.

The above precedence sequence is listed below:

Header ("content-type:text/html; Charset=xxx ")

.. Adddefaultcharset xxx

..

If you are a web programmer, it is recommended that you add a header to each page ("Content-type:text/html;charset=xxx"), so that it can be displayed correctly on any server, portability is also relatively strong.

4) The Default_charset configuration in php.ini:

The Default_charset = "Gb2312″ in php.ini defines the default language character set for PHP. It is generally recommended that this line be commented out so that the browser automatically selects the language based on the charset in the header of the page rather than making a mandatory provision, so that Web services can be provided in multiple languages on the same server.



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.