A The first is the PHP page encoding
1. The encoding of the PHP file itself should match the encoding of the Web page
A. If you want to use gb2312 encoding, then PHP will output headers: header ("content-type:text/html; Charset=gb2312″), static page add , the encoding format for all files is ANSI, which can be opened with Notepad, saved as select encoding ANSI, overwriting the source file.
B. If you want to use UTF-8 encoding, then PHP will output headers: header ("content-type:text/html; Charset=utf-8″), static page add , the encoding format for all files is utf-8. Save As Utf-8 may be a bit troublesome, general Utf-8 file will have a BOM at the beginning, if the use of the session will be a problem, can be saved EditPlus, in the EditPlus, tools---parameter selection, file->utf-8 signature, Select Always Delete, and then save the BOM information can be removed.
2. PHP itself is not Unicode, all functions such as substr should be changed to MB_SUBSTR (need to install mbstring extension), or transcoding with Iconv.
two. PHP's data interaction with MySQL
PHP and database encoding should be consistent
1. Modify the MySQL configuration file My.ini or My.cnf,mysql best with UTF8 encoding
[MySQL]
Default-character-set=utf8
[Mysqld]
Default-character-set=utf8
Default-storage-engine=myisam
Under [Mysqld], add:
Default-collation=utf8_bin
init_connect= ' SET NAMES utf8′
2. Add mysql_query ("Set Names ' code ') before PHP that needs to do the database operation, and the code is consistent with PHP code, if PHP code is gb2312 that MySQL code is gb2312, If it's utf-8, the MySQL code is UTF8, so you don't get garbled when inserting or retrieving data.
three. PHP is related to the operating system
The encoding of Windows and Linux is not the same, in the Windows environment, the function of invoking PHP when the parameter is UTF-8 encoding will be error, such as Move_uploaded_file (), FileSize (), ReadFile (), etc. These functions are often used when handling uploads and downloads, and the following errors may occur when calling:
Warning:move_uploaded_file () [function.move-uploaded-file]:failed to open stream:invalid argument in ...
Warning:move_uploaded_file () [Function.move-uploaded-file]:unable to move ' to ' in ...
Warning:filesize () [Function.filesize]: Stat failed for ...
Warning:readfile () [Function.readfile]: Failed to open stream:invalid argument in.
In the Linux environment with GB2312 encoding although these errors will not occur, but the file name after the save is garbled resulting in unreadable files, you can first convert the parameters to the operating system identification code, encoding conversion can be used mb_convert_encoding (string, new code, the original code) or Iconv (original code, new Code, string), so that the file name stored after processing will not appear garbled, can also read the file, to achieve the Chinese name file upload, download.
In fact, there is a better solution, complete with the system out, also do not have to consider what the system is coding. Can generate a sequence of only letters and numbers as the file name, and the original with the Chinese name in the database, so call Move_uploaded_file () There is no problem, the download only need to change the file name to the original Chinese name. The code to implement the download is as follows
Header ("Pragma:public");
Header ("Expires:0″");
Header ("Cache-component:must-revalidate, post-check=0, Pre-check=0″);
Header ("Content-type: $file _type");
Header ("Content-length: $file _size");
Header ("content-disposition:attachment; Filename=\ "$file _name\");
Header ("Content-transfer-encoding:binary");
ReadFile ($file _path);
$file _type is the file type, $file _name is the original name, $file _path is the address of the file that is saved on the service.
Four. And then I'll summarize why it's garbled.
In general, garbled appearance there are 2 reasons, the first is due to the encoding (charset) set error, causing the browser to parse with the wrong encoding, resulting in a full screen of a messy "heavenly book", followed by the wrong code to open the file, and then save, such as a text file was originally GB2312 encoded, It is opened and saved with UTF-8 encoding. To solve the above garbled problem, you need to know what aspects of development involve coding:
1, file encoding: Refers to the page file (. html,.php, etc.) itself is what encoding to save. Notepad and Dreamweaver automatically recognize the file encoding when they open the page and are therefore less problematic. And Zendstudio does not automatically identify the code, it will only be fixed in accordance with the preferences of the configuration of a certain encoding to open the file, if the working time a note, with the wrong code to open the file, made a change after the save, garbled appear (I have experience).
2, page declaration code: In the HTML code head inside, you can use To tell the browser page adopted what code, the current Chinese website development in XXX is mainly used in GB2312 and UTF-8 two kinds of coding.
3, database Connection code: Refers to the database operation in which encoding and database transfer data, it is important to note that the database itself is not confused with the code, such as MySQL internal default is latin1 encoding, that is, MySQL is latin1 encoding to store data, Data transmitted to MySQL in other encodings is converted to latin1 encoding.
Know the Web development where the coding, also know the reason for garbled: The above 3 encoding settings are inconsistent, because the majority of the encoding is compatible with ASCII, so the English symbol will not appear, Chinese is unlucky.
Five. Battle some common error situations with resolution:
1, the database using UTF8 encoding, and page declaration code is GB2312, which is the most common cause of garbled. At this time in the PHP script, the direct select data is garbled, need to be used before query: mysql_query ("SET NAMES GBK"); To set the MySQL connection code to ensure that the page declaration code is consistent with the connection code set here (GBK is an extension of GB2312). If the page is UTF-8 encoded, you can use: mysql_query ("SET NAMES Utf8″);
Note that it is a UTF8 rather than a general UTF-8. If the code of the page declaration is consistent with the internal encoding of the database, the connection code can be set.
Note: In fact, MySQL data input and output is more complicated than the above, the MySQL configuration file My.ini defined 2 default encoding, respectively [client] in the Default-character-set and [mysqld] Default-character-set to set the default client connection and the encoding used internally by the database. The code we specified above is actually the command line parameter character_set_client of the MySQL client to connect to the server, to tell the MySQL server what encoding the client data is accepted, not the default encoding.
2, the page declaration code and the file itself encoding inconsistent, this situation rarely occurs, because if the code is inconsistent with the page when the browser sees is garbled. More often than not after the release of some minor bugs, the wrong code to open the page and save the resulting. or using some FTP software to directly modify the file online, such as CuteFTP, due to the software encoding configuration error, resulting in the wrong conversion encoding.
3, some rented virtual host friends, clearly the above 3 codes are set correctly or garbled. For example, the Web page is GB2312 encoded, IE and other browser open but always recognized as UTF-8, the page head has been declared to be GB2312, manually modify the browser code for GB2312 after the page display normal. The reason is that the server Apache set the server global default encoding, in Httpd.conf added Adddefaultcharset UTF-8. At this time the server will first send HTTP headers to the browser, the priority is higher than the page stated that the code is high, the natural browser is identified wrong. There are 2 solutions, please add a adddefaultcharset GB2312 to the config file's own virtual machine to cover the global configuration, or configure it in the. htaccess of your own directory.
Summary: In short, to solve the PHP Chinese garbled the best solution is that the page declaration of the Code and the database internal code consistent, if the page to request the number and database internal code inconsistency, set the connection code, mysql_query ("Set NAMES XXX"); XXX is the connection code. Must be able to solve the problem of garbled.
http://www.bkjia.com/PHPjc/320460.html www.bkjia.com true http://www.bkjia.com/PHPjc/320460.html techarticle A. The first is the PHP page encoding 1. The encoding of the PHP file itself and the page encoding should match a. If you want to use gb2312 encoding, then PHP will output headers: header ("content-type:text/html;