/ Published in: PHP
URL: http://www.denhamcoote.com/php-howto-sanitize-database-inputs
Expand |
Embed | Plain Text
function cleanInput($input) { '@<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 ); return $output; } function sanitize($input) { foreach($input as $var=>$val) { $output[$var] = sanitize($val); } } else { } $input = cleanInput($input); } return $output; } //usage $bad_string = "Hi! <script src='http://www.evilsite.com/bad_script.js'></script> It's a good day!"; $_POST = sanitize($_POST); $_GET = sanitize($_GET); $good_string = sanitize($bad_string);
Comments
Subscribe to comments
You need to login to post a comment.

Love this snippet, thanks for posting! One small comment though, does this really need two functions? I'd say it'd be much cleaner and more portable if you combined the two into one sanitize function. Just my $.02