Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the reasons for garbled php pages?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the reasons for garbled php pages". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the reasons for garbled php pages?"

Reasons for php page garbled: 1, web page encoding (charset) setting error, causing the browser to parse with the wrong code; 2, the php file was opened and saved with the wrong code.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

PHP in the actual writing code, there will often be some problems, such as garbled pages and other problems. Next, we will describe in detail the causes of garbled code on PHP pages and related solutions.

Generally speaking, there are two reasons for garbled PHP pages:

Due to the wrong charset setting, the browser parses the code with the wrong code, resulting in a messy "heavenly book" on the screen.

The file is opened in the wrong encoding and saved, such as a text file that was originally encoded in GB2312 but opened in UTF-8 encoding and then saved.

To solve the above PHP page garbled problem, you first need to know which aspects of development involve coding:

Page statement coding: in the HTML code HEAD, it can be used to tell the browser what coding the web page uses. At present, XXX mainly uses GB2312 and UTF-8 coding in the development of Chinese websites.

Database connection coding: refers to which encoding is used to transmit data to the database during database operations. It should be noted here that it should not be confused with the database itself. For example, the internal default of MySQL is latin1 encoding, that is to say, Mysql stores data with latin1 encoding, and data transmitted to Mysql with other codes will be converted into latin1 encoding.

Know where coding is involved in WEB development, we also know the causes of PHP page garbled: the above encoding settings are inconsistent, as most of the various codes are compatible with ASCII, so English symbols will not appear, Chinese is unlucky.

Here are some common error situations and solutions:

1. The database uses UTF8 coding, while the page declaration code is GB2312, which is the most common cause of garbled code. At this time, the SELECT data directly from the PHP script is garbled on the PHP page, which needs to be used before querying:

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 UTF8 rather than the usual UTF-8. If the code stated on the page is consistent with the internal code of the database, the connection code may not be set.

Note: in fact, the data input and output of MYSQL is a little more complex than mentioned above. Two default encodings are defined in the MYSQL configuration file my.ini, namely default-character-set in [client] and default-character-set in [mysqld] to set the default client connection and database internal encodings, respectively. The encoding we specified above is actually the command line parameter character_set_client when the MYSQL client connects to the server to tell the MYSQL server what the client data is encoded instead of using the default encoding.

2. The coding of the page declaration is inconsistent with the coding of the file itself, which rarely occurs, because if the coding is inconsistent, what the artist sees in the browser when doing the page is garbled. More often, it is caused by modifying some small BUG after release, opening the page with the wrong code and then saving it. Or use some FTP software to directly modify the file online, such as CuteFTP, due to the misconfiguration of the software code, resulting in the conversion of the wrong code.

At this point, I believe that you have a deeper understanding of "what are the reasons for garbled php pages?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report