Oh god, please tell me you're doing some type of mysql_escape_string
or mysql_real_escape_string
or AT LEAST addslashes
or addcslashes
to any $_POST
variables before you insert them into a raw MySQL statement?
I think the most secure way to do this is to:
a) use filter_var
or preg_replace
to get rid of extraneous characters from the $_POST['username']
b) SELECT
the row by the username from MySQL (also grabbing the digested password)
c) compare the message digested version of the password from the $_POST
to that of the retrieved row (assuming you don't leave your password cleartext) in your application code, not in the SQL statement
If you do it this way, there's only 1 possible place for injection (username), and it's pretty impossible when you're doing a preg_replace( '/\W/', '', $_POST['username'] )
which removes anything not A-Za-z0-9_- (or change to your username whitelist of characters).
However, if you're doing rock-solid proper sanitization, it really doesn't matter where you do your comparison. Theoretically, though, I'd allow for the least possible interaction with user input and raw SQL statements (i.e. only SELECT
ing by username and comparing outside of your DB).