function clean_for_mysql($string,$max_length) { $in_string = ltrim($string); $in_string = rtrim($in_string); if (round($max_length) < 1) { $max_length = 131072; // 128K } if (strlen($in_string) > $max_length) { $new_string = substr($in_string,0,$max_length); } $new_string = mysql_real_escape_string($new_string); return $new_string; }
For example:
clean_for_mysql("Hack String'", 12);
Will return: Hack String\
(note the ending ' has been cut off)
Which when put in an SQL Query will escape the closing quote and allow injection.
MySQL will automatically cut your string to a certain length if you supply a limit for the field, so there is no need for the insecure substr().
Instead, just use:
mysql_real_escape_string(trim($string))
Use them.
You need to create an account or log in to post comments to this site.