Originally posted by Bob I also do form validation on the PERL / PH level too. I always do this because someone can get the source for your page, modify it by taking out the clientside javascript validation, and then submit crap into your database to corrupt it.
I also commonly use this function when adding stuff to database fields because users may embed script that will be executed when you display the values in a web page.
PHP Code:
function safehtml($str) {
//nuke script and header tags and anything inbetween
$str = preg_replace("'<script[^>]*?>.*?</script>'si", "", $str);
$str = preg_replace("'<head[^>]*?>.*?</head>'si", "", $str);
//listed of tags that will not be striped but whose attributes will be
$allowed = "br|b|i|p|u|a|block|pre|center|hr";
//start nuking those suckers. don you just love MS Word's HTML?
$str = preg_replace("/<((?!\/?($allowed)\b)[^>]*>)/xis", "", $str);
$str = preg_replace("/<($allowed).*?>/i", "<\\1>", $str);
return $str;
}
More handy "safe" functions are here:
http://us4.php.net/strip_tags
Bookmarks