PHP page Encoding declaration method in detail (header or meta)

Source: Internet
Author: User
PHP header to define a PHP page for UTF encoding or GBK encoding

PHP page is UTF encoded
Header ("content-type:text/html; Charset=utf-8 ");

PHP page is GBK encoded
Header ("content-type:text/html; charset=gb2312 ");

PHP page is BIG5 encoded
Header ("content-type:text/html; Charset=big5 ");

Usually the above code is placed on the homepage of the PHP page

The difference between PHP page encoding using header or meta
First, page encoding

1. Use <meta http-equiv= "Content-type" content= "text/html; Charset=xxx "> Label Settings page encoding

The purpose of this tag is to declare the client's browser with what character set encoding to display the page, XXX can be gb2312,gbk,utf-8 (unlike MySQL, MySQL is UTF8) and so on. As a result, most pages can use this method to tell the browser what code to use when displaying this page, so that it does not cause coding errors and garbled characters. But sometimes we will find that with this sentence or not, regardless of xxx is the kind of, the browser is always a kind of coding, this situation I will talk about later.

Note that,<meta> is an HTML message and is simply a declaration that the server has uploaded HTML information to the browser.

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

The function of the header () is to send the information inside the parentheses to the HTTP header.

If the contents of the parentheses are the same as in the text, the function and the <META> tag are basically the same, and the characters are similar to the one seen in the first. But the difference is that if you have this function, the browser will always take the XXX code you require, absolutely will not be disobedient, so this function is very useful. Why is that? Let's talk about the difference between the HTTPS header and the HTML message:

The HTTPS header is a string that the server sends HTML messages to the browser before the HTTP protocol.

Because the META tag is HTML information, the header () sends the content first to the browser, the popular point is the header () priority is higher than meta (I do not know if this can be said). Add a PHP page to both headers ("content-type:text/html; Charset=xxx "), but also <meta http-equiv=" Content-type "content=" text/html; Charset=xxx, the browser will only recognize the HTTP header of the former and not recognize Meta. Of course, this function can only be used within PHP pages.

There is also the question of why the former is absolutely working, and the latter sometimes not. This is the reason for the next Apache to talk about.

3. Adddefaultcharset

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

With a text editor open httpd.conf, 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 Web page file in the HTTPS header of the character set for your default XXX character set. With this line, it is equivalent to adding a row header to each file ("content-type:text/html; Charset=xxx "). This will understand why the obvious meta set is utf-8, can browser always use gb2312 reason.

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

Summarize:

Sort of.

Header ("content-type:text/html; Charset=xxx ")
Adddefaultcharset xxx
<meta http-equiv= "Content-type" content= "text/html; Charset=xxx ">
If you are a Web programmer, add a header to each of your pages ("content-type:text/html; Charset=xxx ") to ensure that it can be displayed correctly on any server and is highly portable.

As for that sentence adddefaultcharset xxx, should not comment on the beholder. Anyway, I have commented out, but I write the page also to write the header (), easy to put on the server can be displayed properly.

Second, the database code

PHP program before querying the database, first execute mysql_query ("SET NAMES xxxx"), where xxxx is your Web page encoding (charset=xxxx), if the page Charset=utf8, then Xxxx=utf8, if the page charset=gb2312, xxxx=gb2312, almost all web programs, there is a connection to the database of common code, placed in a file, in this file, add mysql_query ("set names") on it.

Set NAMES shows what character set is used in the SQL statement sent by the client. Therefore, the set NAMES ' utf-8 ' statement tells the server that "the information coming from this client will be in character set Utf-8". It also specifies a character set for the result that the server sends back to the client. (For example, if you use a SELECT statement, it indicates what character set the column values use.) )

PHP page Encoding Unified

MySQL database encoding, HTML page encoding, PHP or HTML files themselves are encoded to be identical.
1, MySQL database encoding: When the database is set up to specify the encoding (such as GBK_CHINESE_CI), set up a data table, create a field, insert data, do not specify the encoding, will automatically inherit the database encoding.
When the database is connected, there is also code that can be executed after the database is connected
mysql_query (' SET NAMES gbk ');//convert GBK to your code, such as UTF8.

2, the HTML page encoding, refers to the setting of this line:
<meta http-equiv= "Content-type" content= "text/html; CHARSET=GBK "/>

3, PHP or HTML file itself encoding: with EditPlus open PHP file or HTML file, save, select the encoding, if the database and page encoding is GBK, then the code here choose ANSI; If the database and page encoding is Utf-8, then Utf-8 is also selected here.

4, also note that the data passed in JavaScript or flash is UTF-8 encoded, if the database and page encoding is GBK, to transcode, and then write to the database.
Iconv (' Utf-8 ', ' GBK ', $content);

5, in the PHP program, you can add a line to specify the code of the PHP source program:
Header (' content-type:text/html; CHARSET=GBK ');

Related Article

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.