PHP webpage Encoding

Source: Internet
Author: User

Analysis of Apache and PHP webpage Encoding Problems

Let's talk about the encoding of Apache and PHP Web pages. There is also an article about the MySQL Character Set: http://potatows.eeie.cn /? P = 39
Speaking of Apache coding, we need to involve three things.

<META http-equiv = "content-type" content = "text/html; charset = xxx"> tag in http Markup Language
Header ("content-type: text/html; charset = xxx") in PHP; Function
AddDefaultCharset in the Apache configuration file httpd. conf
1. <META http-equiv = "content-type" content = "text/html; charset = xxx"> tag
In order, let's talk about this <META> tag. This tag has many functions. Click here for details.
Today I want to talk about the form mentioned above. <META http-equiv = "content-type" content = "text/html; charset = UTF-8">: Declares the webpage, let the browser on the entire page content using xxx encoding, xxx Can Be GB2312, GBK, UTF-8 (and MySQL, MySQL is UTF8) and so on. Therefore, 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 generate garbage codes. 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 <meta> is html information and is only a declaration. It indicates that the server has transmitted HTML information to the browser.

Ii. header ("content-type: text/html; charset = xxx ");
This function header () sends the information in the brackets to the http header. For more information about how to use this function, click here.
If the content in the brackets is as described in the text, the function is basically the same as that of the <META> tag. You can check the first one and find that the characters are similar. But the difference is that if there is this function, the browser will always use the xxx code you requested, and it will never be disobedient, so this function is very useful. Why? Let's talk about the differences between the HTTPS header and the HTML information:
Reference:
The https header is the string sent by the server before the server sends HTML information to the browser over HTTP.
Because meta tags belong to html information, the content sent by header () First arrives in the browser. The common point is that header () has a higher priority than meta (I don't know if I can do this ). Add a php page with both header ("content-type: text/html; charset = xxx "), and <META http-equiv = "content-type" content = "text/html; charset = xxx">, the browser only recognizes the http header of the former and does not recognize 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.

Iii. adddefacharcharset
The conf folder in the Apache root directory contains the entire Apache configuration file httpd. conf. For specific configuration items, click here (export url1_thread-2674-1-1.html] windows [/url.pdf, export url1_thread-1381-1.html] linux [/url]).
Open httpd. conf in a text editor. Line 1 (different versions may be different) has adddefacharcharset xxx, and xxx is the encoding name. Set the character set in the https header of the webpage file on the server as your default xxx character set. This line adds a header ("content-type: text/html; charset = xxx") to each file "). Now we can see why the meta settings are UTF-8 and the browser can always use gb2312.
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.


Summary:
To sort

Header ("content-type: text/html; charset = xxx ")
Adddefacharcharset xxx
<META http-equiv = "content-type" content = "text/html; charset = xxx">
If you are a web programmer, add a header ("content-type: text/html; charset = xxx") to each page to ensure that it is correctly displayed on any server, high portability.
As for adddefacharcharset xxx, if you want to comment it out, you can see it. I commented it out, but I also need to write the header () to write the page so that it can be properly displayed on different servers.


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.