I have a PHP application where I'm using PHP mysql native library to run and manage queries with the database. In my application, I'm using an abstract layer to validate user input [and any other inputs] against SQL Injection. Some times, in some areas the same input is validated against SQL injection multiple times, which adds additional slashes to the input
For example:
$str = "It's cold";
$str = validate_against_sql($str);//produces It\'s cold
$str = validate_against_sql($str);//produces It\\\'s cold
Which means additional slashes are added to the input/string.
I've googled for 2 hours and did not find a way to do it, and tried to write a function myself that does this process but I'm not familiar with regular expressions and couldn't do it.
Is there a way to add to my "validate_against_sql" function that checks first if the parameter is already valid as a query parameter, so in this case, I would ignore validating it again and prevent adding the slashes?
I mean something like this:
function validate_against_sql($str){
if(!string_already_valid_as_sql_query_parameter($str)){
mysql_real_escape_string($str);
}
}
So, basically I want the "string_already_valid_as_sql_query_parameter" function checks if all special characters are already skipped, and in this case don't validate the same input again.