What characters are NOT escaped with a mysqli prepared statement?

前端 未结 4 1797
独厮守ぢ
独厮守ぢ 2021-01-20 00:47

I\'m trying to harden some of my PHP code and use mysqli prepared statements to better validate user input and prevent injection attacks.

I switched away from mysql

相关标签:
4条回答
  • 2021-01-20 01:07

    % is not an inherently harmful character.

    The question is: why are you using a LIKE in the first place? Are there any circumstances in which you wouldn't require an exact match for username?

    The query should be simply:

    SELECT `salt` FROM admins WHERE `username` = ? LIMIT 1
    

    In that case, if I were to enter %bsmith my username would have to be (literally) "%bsmith" in order for you to find a match.

    0 讨论(0)
  • 2021-01-20 01:24

    You are confusing two different levels of evaluation here.

    The LIKE operator takes a string and evaluates any '%' and '_' as placeholders.

    The job of query parameters is it only to bring values (e.g. strings) verbatim to the database engine, so they cannot be mistaken for SQL code. They don't care how the LIKE operator makes special use of certain characters within the string they've just transported. Everything just works as designed here.

    If you want exact matches, use the = operator in place of LIKE.

    If you must use LIKE (even though your LIMIT 1 indicates otherwise here), escape the the special characters accordingly yourself beforehand.

    0 讨论(0)
  • 2021-01-20 01:24

    These are the characters not escaping by prepared statements % _ \

    0 讨论(0)
  • 2021-01-20 01:27

    It is one who is using LIKE to match a username to blame, not escaping function.

    And, just for your info: native prepared statements do not escape anything.

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