/ Published in: PHP
This function can be used to discard any characters that can be used to manipulate the SQL queries. So, you can use this function to validate your SQL queries against sql injection:
Expand |
Embed | Plain Text
/* I have made the following function that can be used to discard any characters that can be used to manipulate the SQL queries. So, you can use this function to validate your SQL queries against sql injection: example use: if (is_valid($_REQUEST["username"]) == true && is_valid($_REQUEST["pass"]) == true) { //login now } You should still use mysql_real_escape_string() function in your queries to be MORE secure. */ function is_valid($input) { { $loop = true; } if ($loop == true) { foreach($input as $value) { { return false; } else { return true; } } } else { { return false; } else { return true; } } }
Comments
Subscribe to comments
You need to login to post a comment.

wouldn't be too helpful for anyone wanting to allow text comments, emails etc... I used the words "like" "or", "update" and "union" all the time.
Just run your text input through mysqlrealescape_string() (if using mysql)
OR
if you are using mysqli in it's native OOP construct, run all your inputs through mysqli's escape method
[code] $mysqli = new mysqli("localhost", "myuser", "mypassword", "world"); $city = $mysqli->realescapestring($city); [/code]
that will escape the characters needed to break the SQL string for a SQL injection attack to take place.. for example
update users set name='$newname' where id=$id limit 1;
the $newname would need to include a single quote for the SQL engine to parse it as a command instead of a string like this
$newname = "' OR 1=1 drop table users --";
since the single quote is required... the realescapestring() would just escape that single quote and be done with it, no need to string matching and array looping that would flag a lot of false positives :P