Do htmlspecialchars and mysql_real_escape_string keep my PHP code safe from injection?

前端 未结 6 1333
离开以前
离开以前 2020-11-22 04:01

Earlier today a question was asked regarding input validation strategies in web apps.

The top answer, at time of writing, suggests in PHP just using

6条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 04:24

    In addition to Cheekysoft's excellent answer:

    • Yes, they will keep you safe, but only if they're used absolutely correctly. Use them incorrectly and you will still be vulnerable, and may have other problems (for example data corruption)
    • Please use parameterised queries instead (as stated above). You can use them through e.g. PDO or via a wrapper like PEAR DB
    • Make sure that magic_quotes_gpc and magic_quotes_runtime are off at all times, and never get accidentally turned on, not even briefly. These are an early and deeply misguided attempt by PHP's developers to prevent security problems (which destroys data)

    There isn't really a silver bullet for preventing HTML injection (e.g. cross site scripting), but you may be able to achieve it more easily if you're using a library or templating system for outputting HTML. Read the documentation for that for how to escape things appropriately.

    In HTML, things need to be escaped differently depending on context. This is especially true of strings being placed into Javascript.

提交回复
热议问题