PHP Coding Unified and Page Coding declaration method detailed (header or meta) ___ code

Source: Internet
Author: User
Tags php source code

Original address: http://www.jb51.net/article/22501.htm

The difference between PHP page encoding Declaration and PHP page encoding using header or meta

PHP header to define a PHP page for UTF encoding or GBK encoding

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

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

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

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

use header or meta to achieve the difference between PHP page encoding
One, page coding


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

The role of this label is to declare the client's browser with what character set code to display the page, XXX can be for gb2312,gbk,utf-8 (and MySQL is different, MySQL is UTF8) and so on. Therefore, most of the pages can be used in this way to tell the browser to display the page when the code, so that will not cause coding errors generated garbled. But sometimes we will find that there is no, no matter what XXX is, the browser is always a kind of coding, which I will talk about later.

Note that,<meta> is an HTML message, just a statement that works to show that the server has uploaded HTML information to the browser.

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

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

If the contents of the parentheses in the text, that the function and <META> tags are basically the same, you see the first to find the characters are similar. But the difference is that if you have this function, the browser will always use your request for the XXX code, will not be disobedient, so this function is very useful. Why is that so? Well, let's talk about the difference between HTTPS headers and HTML information:

The HTTPS header is a string sent by the server before the HTTP protocol is used to send HTML information to the browser.

Because META tags are HTML information, the header () is sent to the browser first, and the popular point is that header () has precedence over meta (I don't know if that's possible). Join a PHP page with both header ("content-type:text/html; Charset=xxx "), there are <meta http-equiv=" Content-type content= "text/html; Charset=xxx "", the browser will only recognize the former HTTP headers and not to recognize Meta. Of course, this function can only be used within the PHP page.

There is also a question as to why the former works absolutely, and the latter sometimes does not. That's why we're going to talk about Apache next.

3. Adddefaultcharset

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

Open httpd.conf with a text editor, 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 within the page file HTTPS header of the character set for your default XXX character set. With this line, it is equivalent to adding a line header to each file ("content-type:text/html; Charset=xxx "). This makes clear why obviously Meta set is utf-8, but browsers always use the gb2312 reason.

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

Summary:

A sort of.

Header ("content-type:text/html; Charset=xxx ")
Adddefaultcharset xxx
<meta http-equiv= "Content-type" content= "text/html; Charset=xxx ">
If you're a web programmer, add a header to every page you have ("content-type:text/html; Charset=xxx "), to ensure that it can be correctly displayed on any server, the portability of strong.

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

second, the database coding

PHP program before querying the database, first executes mysql_query ("SET NAMES xxxx"), where xxxx is your page encoding (CHARSET=XXXX), if the page Charset=utf8, then Xxxx=utf8, if the page charset=gb2312, then xxxx=gb2312, almost all web programs, have a piece of public code connected to the database, put in a file, in this file, add mysql_query ("set names") on it.

The set NAMES displays what character set is used in the SQL statements sent by the client. Therefore, the set NAMES ' utf-8 ' statement tells the server that "future information from this client will be based on the character set Utf-8". It also specifies the character set for the result that the server sends back to the client. (For example, if you use a SELECT statement, it represents what character set the column value uses.) )

PHP page code unification

MySQL database code, HTML page encoding, PHP or HTML file itself to encode all the same.
1, MySQL database encoding: the establishment of the database to specify the encoding (such as GBK_CHINESE_CI), the establishment of data tables, the establishment of fields, insert data do not specify the encoding, will automatically inherit the database encoding.
When the database is connected, there is also an encoding that can be executed after the database is connected
mysql_query (' SET NAMES gbk ');//convert GBK to your code, such as UTF8.

2, 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 when, select the encoding, if the database and page encoding is GBK, then here's the code select ANSI, if the database and page encoding is Utf-8, then here also choose Utf-8.

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

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

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.