PHP's precautions for SQL injection.

Source: Internet
Author: User

Recently in the use of the framework is still a little uneasy, do not know the framework of the designers have not considered the problem of sql-injection, I need to do in the top floor needs to do some necessary filtering and so on, so I deliberately to StackOverflow to see the next, really benefited a lot, Then I went to see the internal method of the DB Library under the framework, and then it was more reassuring. Share some of the programs that PHP programmers at home and abroad are dealing with sql-injection.

Abroad is generally recommended, as long as you are using the query should be done two points: 1. prepared Statements(Ready statement) 2. Parameterized queries (parameterized query request).

I didn't understand what that meant at first, but then I looked at them for example and I probably knew it. For a more secure SQL, you need to start by preparing the variables for the query. Such as:

$name $_post [' name ']; $sql = ' SELECT * from user where name '. $name;

Then the best thing is to deal with $name first,

$name mysql_real_escape_string ($_post[' name ']);

Then, let the requested variable become a parameter, not the SQL language itself.

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

Of course, this kind of writing is still relatively rough.

Therefore, it is generally recommended to use PDO or the mysqli prepare () Excute () method.

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

About PDO::p Repare ()

The advantage of this is that you no longer need to worry that the query request will insert some SQL statements, as these statements will be treated as request variables (a string or a number) and no longer be mistaken for the SQL language itself. This can greatly reduce the chances of SQL injection.

PHP's precautions for 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.