PHP application of various garbled code problem solution

  • 2020-03-31 20:39:57
  • OfStack

1) use the label to set the page encoding

The purpose of this tag is to declare what character set the client's browser will use to display the page, XXX can be GB2312, GBK, utf-8 (unlike MySQL, which is UTF8), and so on. As a result, most pages can use this method to tell the browser what code to use when displaying the page, so as not to cause coding errors and generate garbled code. However, sometimes we find that this sentence still does not work, no matter which XXX is, the browser is always using a code, which I will talk about later.

Note that this is HTML information, just a declaration that the server has passed the HTML information to the browser.

2) header (" content-type: text/HTML. Charset = XXX ");

The function header() is used to send the information in parentheses to the HTTP header. If the contents of the brackets are as described in the text, then the function is basically the same as the label, and you can see that the characters are the same as the first one. But the difference is that if you have this function, the browser will always use the XXX code you asked for, and will never disobey, so this function is very useful. Why is that? Here's the difference between HTTP headers and HTML:

The HTTP header is the string that the server sends before it sends the 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 common point is that the header() has a higher priority than that. Suppose a PHP page has headers ("content-type:text/ HTML; Charset = XXX "), again, browsers only recognize the former HTTP header and not meta. Of course, this function can only be used within PHP pages.

It also leaves open the question, why does the former absolutely work and the latter sometimes not? This is where Apache comes in.

3) AddDefaultCharset

In the conf folder of the Apache root directory, httpd.conf contains the entire Apache configuration document.

Open httpd.conf with a text editor. Line 708 (different versions may vary) has AddDefaultCharset XXX, with XXX as the encoding name. Set the character set in the HTTP header of the entire web file on the server to your default XXX character set. Having this line is like adding a header to every file ("content-type:text/ HTML; Charset = XXX "). This explains why the browser still USES gb2312 when it is clearly set to utf-8.

If the page has a header("content-type:text/ HTML; Charset = XXX "), change the default character set to the character set you set, so this function is always useful. If you put a "#" in front of AddDefaultCharset XXX, comment out this sentence, and there is no header in the page ("content-type... "). ), which is when the meta tag comes into play.

The above priorities are listed below:

. The header (" content-type: text/HTML; Charset = XXX ")

. AddDefaultCharset XXX

.

If you are a web programmer, it is recommended that you add a header to every page ("content-type:text/ HTML; Charset = XXX "), so that it can be guaranteed to display correctly in any server, portability is also relatively strong.

4) configuration of default_charset in php.ini:

The default_charset = "gb2312" in php.ini defines the default language character set for PHP. It is recommended that you comment out this line and let the browser automatically select the language based on the charset in the header of the page, rather than making it mandatory, so that multiple languages can be served on the same server.

(link: #)
(link: #)

Related articles: