What are the best methods of sanitizing values from a database (in php) if they are to be used in inputs like textareas?
For example, when inserting data, I can strip tags and quotes and replace them with html char codes and then use mysql_real_escape_string right before insertion.
When retrieving that data back, I need it to show up in a textarea. How can I do this and still avoid XSS? (Ex. you could easily type in
</textarea><script type='text/javascript'> Malicious Code</script><textarea>
) and cause problems.
Thanks!
I think i would prefer a combo of
filter_varandurl_decodeif you want to use a pure simple php SolutionReason
Imagine an impute like this
If i use
strip_tagsOutput
if i use
htmlspecialcharsOutput
My Choice
Output
If form is using
GETinstead ofPOSTsome can till escape if it is url encoded , you are able to get a minimal information and make sure the final text is harmlessThe are also enough class online to help you do filter see
http://www.phpclasses.org/package/2189-PHP-Filter-out-unwanted-PHP-Javascript-HTML-tags-.html
http://htmlpurifier.org/