How do you prevent SQL injection in LAMP applications?

后端 未结 5 1093
隐瞒了意图╮
隐瞒了意图╮ 2020-12-20 16:31

Here are a few possibilities to get the conversation started:

  1. Escape all input upon initialization.
  2. Escape each value, preferably when generating the
相关标签:
5条回答
  • 2020-12-20 16:49

    as @Rob Walker states, parameterized queries are your best bet. If you're using the latest and greatest PHP, I'd highly recommend taking a look at PDO (PHP Data Objects). This is a native database abstraction library that has support for a wide range of databases (including MySQL of course) as well as prepared statements with named parameters.

    0 讨论(0)
  • 2020-12-20 16:49

    Prepared statements are the best answer. You have testing because you can make mistakes!

    See this question.

    0 讨论(0)
  • 2020-12-20 16:59

    PDO may be worth it some day, but it's not just there yet. It's a DBAL and it's strengh is (supposedly) to make switching between vendors more easier. It's not really build to catch SQL injections.

    Anyhow, you want to escape and sanatize your inputs, using prepared statements could be a good measure (I second that). Although I believe it's much easier, e.g. by utilizing filter.

    0 讨论(0)
  • I've always used the first solution because 99% of the time, variables in $_GET, $_POST, and $_COOKIE are never outputted to the browser. You also won't ever mistakenly write code with an SQL injection (unless you don't use quotes in the query), whereas with the second solution you could easily forget to escape one of your strings eventually.

    Actually, the reason I've always done it that way was because all my sites had the magic_quotes setting on by default, and once you've written a lot of code using one of those two solutions, it takes a lot of work to change to the other one.

    0 讨论(0)
  • 2020-12-20 17:11

    I would go with using prepared statements. If you want to use prepared statements, you probably want to check out the PDO functions for PHP. Not only does this let you easily run prepared statements, it also lets you be a little more database agnostic by not calling functions that begin with mysql_, mysqli_, or pgsql_.

    0 讨论(0)
提交回复
热议问题