Jump to content

Allowing comments on website... how should I validate/filter the input... ?


cgm225

Recommended Posts

I want to allow users to leave comments on my website, and allow BBC in the comments.  First, what actions should I be doing on the user provided data (i.e. the comment) to make sure its safe before I store it in my database and then for query when I display it on the website?  Restated, how should I be filtering and/or validating the comment data?  Also, how do I transform BBC into actual HTML elements when I output it to the website?

so i assume you are running a strip_tags on the raw comment, incase someone does want to slip some html in there, that will leave you with just bbc, now make yourself a quick array of what you want to allow in your bbc like this

 

$bbc = array("[b]", "[i]", "[h1]"); // add closing tags too

then a replace with array

$html  = array("<b>", "<i>", "<h1>");

then

$html_comment = str_replace($bbc, $html, $comment);

now if somebody puts valid (meaning in your $bbc array) bbc in their comment then it replaces it, if it is invalid it stays as bbc and is displayed on the web as such,

 

html would look like this

<b>I think this is </b> [bad bbc]

and print in the comment like:

I think this is [bad bbc]

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.