Home >Backend Development >PHP Tutorial >Solutions to garbled code problems in various php applications_PHP Tutorial
1) Use the tag to set the page encoding
The function of this tag is to declare what character set encoding the client's browser uses to display the page. xxx can be GB2312, GBK, UTF-8 (and MySQL is different, MySQL is UTF8) and so on. Therefore, most pages can use this method to tell the browser what encoding to use when displaying this page, so as to avoid encoding errors and garbled characters. But sometimes we will find that this sentence still doesn't work. No matter which xxx is, the browser always uses the same encoding. I will talk about this later.
Please note that it belongs to HTML information and is just a statement, which only indicates that the server has passed the HTML information to the browser.
2) header("content-type:text/html; charset=xxx");
The function of this function header() is to convert the information in the brackets Sent to http headers. If the content in the brackets is as mentioned in the article, the function is basically the same as the label. If you compare the first one, you will find that the characters are similar. But the difference is that if there is this function, the browser will always use the xxx encoding you requested and will never be disobedient, so this function is very useful. Why is this happening? Then we have to talk about the difference between http headers and HTML information:
http header is a string sent by the server before sending HTML information to the browser using the http protocol. The tag belongs to HTML information, so the content sent by header() reaches the browser first. The popular point is that header() has a higher priority (I don’t know if I can say this). If a php page has both header("content-type:text/html;charset=xxx") and header("content-type:text/html;charset=xxx"), the browser will only recognize the former http header and not the meta. Of course, this function can only be used within php pages.
There is also a question left, why does the former definitely work, but the latter sometimes does not work? This is the reason why we want to talk about Apache next.
3) AddDefaultCharset
In the conf folder in the Apache root directory, there is the entire Apache configuration document httpd.conf.
Open httpd.conf with a text editor. Line 708 (different versions may be different) contains AddDefaultCharset xxx, where xxx is the encoding name. The meaning of this line of code: Set the character set in the http header of the web page file in the entire server to your default xxx character set. Having this line is equivalent to adding a line of header("content-type:text/html; charset=xxx") to each file. Now you can understand why the browser always uses gb2312 even though it is set to utf-8.
If there is header("content-type:text/html; charset=xxx") in the web page, the default character set will be changed to the character set you set, so this function will always be useful. If you add a "#" in front of AddDefaultCharset xxx, comment out this sentence, and the page does not contain header ("content-type..."), then it is the meta tag's turn to take effect.
The above priority is listed below:
.. header("content-type:text/html; charset=xxx")
.. AddDefaultCharset xxx
..
If you are a web programmer, it is recommended to add a header ("content-type: text/html; charset=xxx") to each of your pages, so that you can ensure It can be displayed correctly on any server and has strong portability.
4) default_charset configuration in php.ini:
default_charset = "gb2312" in php.ini defines the default language character set of php. It is generally recommended to comment out this line and let the browser automatically select the language based on the charset in the web page header instead of making a mandatory requirement. This way, web services in multiple languages can be provided on the same server.
Solution to the problem of garbled Chinese characters intercepted by PHP Application of mb_substr function
A collection of mysql Chinese garbled code solutions