Php solves the garbled problem of various applications

Source: Internet
Author: User
1) use tags to set page encoding the role of this tag is to declare the client browser with what character set encoding to display the page, xxx can be GB2312, GBK, UTF-8 (and MySQL different, M

1) use tags to set page encoding

The role of this label is to declare the client browser with what character set encoding to display the page, xxx can be GB2312, GBK, UTF-8 (and MySQL, MySQL is UTF8) and so on, so, most pages can use this method to tell the browser what encoding is used to display the page, so as not to cause code errors and garbled code, however, sometimes we will find that this sentence still does not work. no matter which type of xxx is, the browser always uses an encoding. I will talk about this later.

Note that HTML information is only a declaration, indicating that the server has passed the HTML information to the browser.

2) header ("content-type: text/html; charset = xxx ");

This function header () is used to send the information in the brackets to the http header. if the content in the brackets is as described in this article, the functions and labels are basically the same, we can see from the first query that all the characters are similar, but the difference is that if there is a function, the browser will always use the xxx code you requested and will never be disobedient, therefore, this function is very useful. why? Let's talk about the differences between http headers and HTML information:

The http header is the string sent by the server before the server sends HTML information to the browser over http, and the tag belongs to the HTML information. Therefore, the content sent by the header () first reaches the browser, in general, the header () has a higher priority than (I don't know if I can do this). If a php page has both header ("content-type: text/html; charset = xxx "), with meta, the browser only recognizes the http header instead of the meta. of course, this function can only be used on the php page.

There is also a problem. why is the former absolutely effective, while the latter sometimes does not? This is the reason for Apache.

3) adddefacharcharset

The conf folder in the Apache root directory contains the entire Apache configuration file httpd. conf.

Open httpd in a text editor. conf, line 708th (different versions may be different) has adddefacharcharset xxx, xxx is the encoding name, this line of code means: set the character set in the http header of the webpage file on the server as your default xxx character set. This line is equivalent to adding a line of header ("content-type: text/html; charset = xxx") to each file. now you can see why UTF-8 is set, the reason why gb2312 is always available in browsers.

If the webpage contains a header ("content-type: text/html; charset = xxx"), change the default character set to the character set you set, so this function will always be useful, add "#" before adddefacharcharset xxx, comment out this sentence, and the page does not contain header ("content-type... "), Then it is the time for the meta tag to take effect.

The preceding priorities are listed below:

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

.. Adddefacharcharset xxx

If you are a web programmer, we recommend that you add a header ("content-type: text/html; charset = xxx") to each page "), in this way, it can be correctly displayed on any server, and the portability is also strong.

4) default_charset configuration in php. ini:

Php. default_charset = "gb2312" in ini defines the default language character set of php. it is generally recommended that you comment out this line so that the browser can automatically select a language based on the charset in the header of the web page instead of making a mandatory rule, in this way, you can provide web services 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.