Using MySQL PDO to prevent SQL injection

Source: Internet
Author: User

Skills:

1. PHP upgrade to 5.3.6+, production environment is highly recommended to upgrade to PHP 5.3.9+ PHP 5.4+,php 5.3.8 There is a fatal hash collision vulnerability.

2. If you are using PHP 5.3.6+, specify the charset attribute in the PDO DSN
3. If PHP 5.3.6 and previous versions are used, set the Pdo::attr_emulate_prepares parameter to False (that is, the variable is handled by MySQL), PHP 5.3.6 or later has already dealt with this issue, Whether you are using a local emulation prepare or a prepare that calls MySQL server. Specifying CharSet in DSN is not valid, and the execution of set names <charset> is essential.

4. If PHP 5.3.6 and previous versions are used, because the YII framework does not set the value of Attr_emulate_prepares by default, specify the value of Emulateprepare in the database configuration file as false.

So, if you specify CharSet in DSN, do you still need to perform set names <charset>?
Yes, it can't be saved. Set names <charset> actually has two functions:
A. Tell MySQL server what the encoding is for the client (PHP program) to submit to it
B. Tell MySQL server what is the encoding of the results required by the client
In other words, if the data table uses the GBK character set, and the PHP program uses UTF-8 encoding, we run the set names UTF8 before executing the query, telling MySQL server to encode correctly, without having to encode the conversion in the program. This way we submit the query to MySQL server with Utf-8 encoding, and the result will be UTF-8 encoding. Eliminates the conversion coding problem in the program, do not have the doubt, this does not produce garbled.

What is the role of specifying CharSet in DSN? Just tell PDO that the local drive escapes using the specified character set (not setting the MySQL server communication character set), setting the MySQL server communication character set, and using the set names <charset> directive.

Reprint website: http://www.jb51.net/article/56612.htm

Using MySQL PDO to prevent SQL injection

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.