Posted By

stz184 on 10/08/10


Tagged

get post cookie String sanitize


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

jplr


String to database (sanitize string for inserting in database)


 / Published in: PHP
 

This function can be used to sanitize single string variable or massive like POST, GET, COOKIE. It performs magic quotes gpc check, strip tags, trim and escape the dangerous signs with mysqlrealescape_string.

  1. function str2db($input, $strip_tags=true) {
  2. if(is_array($input)) {
  3. foreach($input as $key => $value) {
  4. $input[$key] = str2db($value);
  5. }
  6. } else {
  7. if(ini_get('magic_quotes_sybase')){
  8. $input = str_replace("''", "'", $input);
  9. }
  10. else {
  11. $input = stripslashes($input);
  12. }
  13. }
  14. if($strip_tags) {
  15. $input = strip_tags($input);
  16. }
  17. $input = mysql_real_escape_string($input);
  18. $input = trim($input);
  19. }
  20. return $input;
  21. }

Report this snippet  

You need to login to post a comment.