Jump to content

Can you apply mysqli_real_escape_string to the entire POST array?


blurredvision

Recommended Posts

I have quite a large form that returns a few dozen values through the POST array.  The only way I've ever tried to prevent injection attacks is applying the real_escape_string function to individual variables.  Can I simply do this to the entire POST array, then use associative values to input into the database?

Yes... but try to remember that it does apply to the WHOLE post array, even ones you may not want it to apply to.

 

<?php
foreach($_POST as $var => $val)
{
   $_POST[$var] = mysqli_real_escape_string($connection, $val);
}
?>

If u made a class to handle processing forms you could have

<?php
class process_form{

public function get_inputs(){
  foreach($_POST as $key=>$value){
       $this->inputs[$key] = $value;
}
public function clean_inputs(){
  foreach($this->$inputs as $key=>$value){
         $inputs[$key] = mysql_real_escape_string($value);
 }
}
?>

 

Yes... but try to remember that it does apply to the WHOLE post array, even ones you may not want it to apply to.

 

This is a good reason not to provide blanket alterations to super global arrays ($_GET $_POST $_FILES $_SESSION etc.) because you never know in the future if you don't want to touch one.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.