I'm trying to prevent my login script from being vulnerable to MySQL injection attacks. I'm wondering the following:
A) What other validation should I be performing besides removing "bad" characters and checking the string lengths?
I know a preg will be quicker for replacing strings, I just wanted to illustrate all that I have done here for quicker analysis.
if (($_POST['username']) && ($_POST['password'])) {
$username = str_replace(';','', $_POST['username']);
$username = str_replace('\'','', $username);
$username = str_replace('"','', $username);
$username = str_replace('>','', $username);
$username = str_replace('<','', $username);
$password = str_replace(';','', $_POST['password']);
$password = str_replace('\'','', $password);
$password = str_replace('"','', $password);
$password = str_replace('>','', $password);
$password = str_replace('<','', $password);
if (strlen($username)>10 || strlen($password)>10) {
echo "invalid login";
} else {
$query = mysql_query("select id from sample_table where username = '$username' and password = '$password'");
if (mysql_num_rows($query)) {
// start a session
// header location to members.php page
exit();
} else {
echo "invalid login";
} } }
I have found many threads on this but most vary in answers and opinons. I guess I'm really looking for a "checklist" of things that need validated before the MySQL query is run. Any help would be greatly appreciated.