SQL injection is in most cases easily avoided using prepared statements.
XSS is more complicated if you plan to allow users to host HTML markup. You need to remove all <script> tags, all on* attributes from tags, all javascript: urls, and even then, it may not be fully guaranteed to make the input HTML safe. There are libraries, such as HTMLPurifier , that can help, but as long as you allow HTML, you risk allowing yourself something malicious.
You can use a library that instead implements something like markdown or wikitext. This severely limits what users can enter, while allowing them to tag content to some extent. It is not fully functional (people can still post links to malicious sites and hope that users will come to them, that some of them will be naive enough, and in fact you wonβt be able to use a rich editor such as TinyMCE, a plugin, but this a much simpler task for sanitizing markdowns than for sanitizing HTML.
source share