Jump to content

SQL Injection, is this effective?


kevinkorb

Recommended Posts

Is this effective in preventing SQL injections.. it seems like it would be to me.  What would make this not generally ideal to do before entering user submitted data into the database?

[code]
<?php
Class Filter {
  public static function clean_array($array) {
    foreach($array AS $key => $val) {
        $array[$key] = mysqli_real_escape_string($val);
    }
  }
}

// Then cleaning the post array.

$clean = Filter::clean_array($_POST);

$query = "INSERT INTO table (f1,f2,f3) VALUES ('{$clean['f1']'}, '{$clean['f2']'}, '{$clean['f3']'});";
mysqli_query($db, $query);

?>
[/code]
Link to comment
Share on other sites

Ya, but the good thing about the other one is you can litter that all throughout your project.  If you find a better escaping method you can just change the 'clean_array()' method once to use whatever logic you'd use to clean all your arrays.

Abstraction

But I you made a good point with the array_map function. 

So change the class to be
[code]
<?php
Class Filter {
  public static function clean_array($array) {
    return array_map('mysql_real_escape_string', $array);
  }
}
?>
[/code]
Link to comment
Share on other sites

mysql_real_escape_string() gets all the characters that causes a security risk. An important thing to note is that the characters % and _ which are considered as wildcards if combined with GRANT, LIKE and REVOKE will not be escaped so if you are going to put a string into a such query you must manually escape those characters as well.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.