Php SQL injection prevention measures.

Source: Internet
Author: User
Tags sql injection prevention
Php SQL injection prevention measures. Recently, I am still a little upset when using the framework. I don't know if the framework designer has taken into account the SQL-Injection issue. I don't need to perform necessary filtering on the top layer, as a result, I went to StackOverflow and saw it, which really benefited a lot. then I went to the internal methods of the DB database in the framework, and then I felt at ease. I will share some solutions for PHP programmers at home and abroad to handle SQL-Injection.


It is widely recommended in foreign countries. as long as you are using queries, you should achieve two points: 1.Prepared statements(Prepared statement) 2.Parameterized queries(Parameterized query request).

I didn't understand what it meant at the beginning, and I will probably see them as an example later. For a safer SQL statement, you must first prepare the queried variables. For example:

$name = $_POST['name'];$sql = 'select * from user where name'.$name;

It is best to process $ name first,

$name = mysql_real_escape_string($_POST['name']);

Then, let the requested variables become parameters, rather than the SQL language itself.

$sql = 'select * from user where name=\''.$name.'\'';

Of course, this writing method is still rough.

Therefore, the PDO or MYSQLI prepare () excute () method is generally recommended.

$stmt = $pdo->prepare('SELECT * FROM user WHERE name = :name');$stmt->execute(array('name' => $name));

About PDO: prepare ()

The advantage of this is that you no longer need to worry about inserting some SQL statements into the query request, because these statements will be treated as request variables (a string or number ), it is no longer mistaken for the SQL language itself. This greatly reduces the chance of 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.