Using php filter_var with mysql_real_escape_string

后端 未结 4 864
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-13 16:28

I would like to start my question by saying, I realize PDO/mysqli is the new standard and has been widely covered on SO. However in this particular case I dont have time to conv

相关标签:
4条回答
  • 2021-02-13 16:57

    Your main concern should be this outdated approach, which is leaving your client's site vulnerable to SQL injection attack. While regarding this trifle matter of using yet another irrelevant function, there is nothing to worry about.

    0 讨论(0)
  • 2021-02-13 17:00

    Sanitising a string serves to make it conform to certain expectations. FILTER_SANITIZE_EMAIL removes any characters from a string which would be invalid in an email. The result is (supposedly) guaranteed to conform to email address syntax. How useful randomly removing characters from a string is I'll leave up to you. (Hint: I don't think it's very useful at all; you should rather reject invalid addresses than to transform them into random results. I give you an invalid email address, you hammer it into some shape that resembles an email address, now how do you know you'll be able to send me an email...?!)

    mysql_real_escape_string is there to ensure that an arbitrary string does not violate SQL's string literal syntax by escaping all escape-worthy characters. Assuming you're using it correctly (lots of pitfalls mysql has, which is why it's deprecated...), there's nothing you can do to its input that would make it fail. You give it any arbitrary string, it returns you the escaped version, period.

    As such, in general, yes, what you're doing is fine. If mysql_real_escape_string is the last thing you do to your string before interpolating it into an SQL string literal, then it's fine.

    0 讨论(0)
  • 2021-02-13 17:02

    In this case, if the filter fails the query will be:

    SELECT email FROM members WHERE email = '0'
    

    So, not much to worry about the query; it just won't return any results.

    Unless of course, you have a similar insert / update query.

    In which case, you could have plenty of rows in the database where the email is '0'

    0 讨论(0)
  • 2021-02-13 17:08

    Using both is fine, but what you should really be doing is not to use mysql_ at all. You should instead use PDO or mysqli_ with prepared statements to avoid SQL injection.

    See How can I prevent SQL injection in PHP?

    Or for a guide of how to switch to MySQLi the see Can I blindly replace all mysql_ functions with mysqli_?

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