views:

179

answers:

4

On a PHP file, I receives more than 20 variables coming from the client(submitted via a web form) and I have to apply mysql_real_escape_string() more than 20 times, it is quite troublesome, is there a better way to do this job?

+1  A: 

No, that is the best way. As answered in this old question, you should always use whatever tools the language/system has available for you.

However, your issue still remains about it being tedious. I'd suggest a loop. Assuming your variables are in $_POST:

$vars = array("foo", "bar", "baz"); // names of variables
foreach ($vars as $var) {
    // tricky $$ usage will create the variables
    // $foo, $bar, etc., with the escaped values.
    ${$var} = mysql_real_escape_string($_POST[$var]);
    // you could also store an array of inputs, like $inputs[$var] = ...;
}
jtbandes
Urgh, don't pollute the namespace like that.
notJim
RageZ
@jtbandes: sorry for the wrong editing, yeah right polluting the name space is really nice.
RageZ
+1  A: 

like, foreach?

$names = array('foo', 'bar', 'baz');
$inputs = array();
foreach ($names as $name) {
   $inputs[$name] = mysql_real_escape_string($_POST[$name]);
}
just somebody
+4  A: 

you can use array_map also

$_POST = array_map('mysql_real_escape_string',$_POST);
RageZ
You mean `array_map('mysql_real_escape_string', $_POST)`, right? :-)
Inshallah
@inshallah: Good point I have kind of mistaken both ^^ but array_map would also work .
RageZ
Not "also"! **Only** :-). Check the docs, `array_walk()` will not collect the output from applying the `mysql_real_escape_string()`.
Inshallah
right^^ so `array_map` ^^
RageZ
@inshallah: I own you on this one ^^
RageZ
Ach! But change the link too, it still points to `array_walk`.
Inshallah
@inshallah: I think we *might* be ok
RageZ
Applying mysql_real_escape_string to the POST array is entirely the wrong time for it: you are treating an output issue (injecting text into SQL string literals) as an input issue (taking user submissions). When you add a variable to a query that doesn't come from the POST array, you will have failed to escape it. When you output a variable from the POST array to the page, you will get unwanted slashes. SQL-string-literal escaping must be done when making SQL and only then.
bobince
@bobince, yes that's very true, but I don't think the answer deserves to be downvoted for that oversight, since it doesn't focus on the security issue but rather on the eliminating repetition through `array_map()`. You can replace the assignment to `$_POST` with something else (like directly concatenating into the query with `implode(....)`), and this answer will be just as valid.
Inshallah
also to be noted there is the `filter` extension: http://php.net/manual/en/book.filter.php
RageZ
+3  A: 

is there a better way to do this job?

Certainly: parameterised queries.

They're a little bit wordy in PHP, and unfortunately since they require you to move to mysqli (or some other data access layer that provides the feature, and maybe others like database-independence), instead of the old mysql_ functions there would have to be some rewriting. But taking the SQL string literal escaping out of your application and putting it in the data access layer where it belongs is a big improvement.

bobince
+1 I've waited for this :-)
Inshallah
+1 for `PDO` and `mysqli`
RageZ