Tim C
Tim C

Reputation: 5714

Using php filter_var with mysql_real_escape_string

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 convert all queries to PDO before launching the clients site.

The following has been used throughout most of the queries on the site (not by me may I add)

  $userEmail = filter_var($_POST['fEmail'], FILTER_SANITIZE_EMAIL);
   $userEmail = mysql_real_escape_string($userEmail);
   $sql ="SELECT email FROM members WHERE email = '$userEmail'";
   :
   :

I would like to know:

Is it good / okay practise to use filter_var and mysql_real_escape_string together as in the example above? My main concern is, can these two functions be used together or cause some sort of conflict / bug when executing / uploading to DB?

Also is there any sort of benefit in using both?

Thanks in advance

Upvotes: 13

Views: 1994

Answers (4)

andrew
andrew

Reputation: 9583

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'

Upvotes: 3

worldofjr
worldofjr

Reputation: 3886

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_?

Upvotes: 2

deceze
deceze

Reputation: 522402

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.

Upvotes: 16

Your Common Sense
Your Common Sense

Reputation: 157909

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.

Upvotes: -3

Related Questions