Possible Duplicate:
What's the best method for sanitizing user input with PHP?
I'm using TinyMCE to allow uses to edit a small portion of a web page.
This field is saved to a database.
Is there a regular expression I can use to clean the incoming HTML code so it avoids any XSS/NULL/DROP TABLES kind of attacks?
I've done this on single line inputs text/numbers etc, but not sure how to go about this when receiving an HTML string.
I'd recommend playing with HTMLPurifier.
You can use the PHP functions: striptags
and htmlspecialchars
:
http://php.net/manual/en/function.strip-tags.php
You can use this -
function safe_sql($obj)
{
$obj = htmlspecialchars($obj);
$obj = str_replace('"',""",$obj);
$obj = str_replace("'","'",$obj);
$obj = str_replace("`","`",$obj);
$obj = mysql_real_escape_string($obj);
return $obj;
}
I'm using it and it's working fine. And you can also use this function to make it normal(after you pull the data from the database) -
function to_Normal($data)
{
$data = htmlspecialchars_decode($data);
$data = str_replace(""",'"',$data);
$data = str_replace("'","'",$data);
$data = str_replace("`","`",$data);
$data = nl2br($data);
return $data;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With