Sanitize user inputs using php

function cleaninput($input) {

  $search = array(
    '@<script[^>]*?>.*?</script>@si',   // Strip out javascript
    '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
    '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
    '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments
  );

    $output = preg_replace($search, '', $input);
    return $output;
  }

function sanitize($input) {
    if (is_array($input)) {
        foreach($input as $var=>$val) {
            $output[$var] = sanitize($val);
        }
    }
    else {
        if (get_magic_quotes_gpc()) {
            $input = stripslashes($input);
        }
        $input  = cleaninput($input);
        $output = mysql_real_escape_string($input);
    }
    return $output;
}

// Usage:
$bad_string = "Hi! <script src='http://www.evilsite.com/bad_script.js'></script> It's a good day!";
  $good_string = sanitize($bad_string);
  // $good_string returns "Hi! It\'s a good day!"

  // Also use for getting POST/GET variables
  $_POST = sanitize($_POST);
  $_GET  = sanitize($_GET);
Comments
11 Responses to “Sanitize user inputs using php”
  1. Valerie says:

    As I web site possessor I believe the content material here is rattling great , appreciate it for your efforts. You should keep it up forever! Best of luck.

  2. Makeup tips says:

    An interesting discussion is worth comment. I think that you should write more on this topic, it might not be a taboo subject but generally people are not enough to speak on such topics. To the next. Cheers

  3. excellent post. i want to thank you for this informative read, i really appreciate sharing this great post. keep up your work. lista de emails lista de emails lista de emails lista de emails lista de emails

  4. I’m still learning from you, as I’m trying to achieve my goals. I definitely enjoy reading all that is posted on your site.Keep the tips coming. I loved it!

  5. I simply want to say I am just all new to blogging and site-building and truly savored this web site. Almost certainly I’m planning to bookmark your blog post . You really come with incredible well written articles. Regards for sharing with us your website.

  6. Wow! This could be one particular of the most helpful blogs We have ever arrive across on this subject. Basically Great. I am also a specialist in this topic therefore I can understand your effort.
    chanel wallet http://chanelwallet.ezweb123.com/

  7. Rae Mascarenas says:

    Good site you’ve got here.. It’s hard to find good quality writing like yours these days. I honestly appreciate individuals like you! Take care!!

  8. Sammy Naidu says:

    I just want to tell you that I am beginner to blogs and absolutely savored your blog. Very likely I’m likely to bookmark your site . You definitely come with remarkable articles. Thank you for sharing with us your blog.

  9. Very interesting topic, regards for putting up.

  10. IBCbet says:

    Very interesting subject, regards for putting up.

  11. SEO Murah says:

    Very interesting subject, appreciate it for putting up.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.