PHP header to solve garbled problems and other garbled problems

Source: Internet
Author: User
Transfer from http://www.jb51.net/article/22501.htm

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 the label to set the 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 the HTML information is only a declaration, which works to indicate 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 label are basically the same, and the characters are similar to the first one. 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 "), and also , the browser will only recognize the former HTTP header and do 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.
Summary:
sort of.
header ("content-type:text/html; Charset=xxx ")
adddefaultcharset 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, then 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:

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, The Utf-8 is also chosen 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 ');

The above describes the PHP header to solve the garbled problem and other garbled problems, including the content, I hope that the PHP tutorial interested friends helpful.

  • 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.