There are three main solutions:
- Remove all HTML tags from the message. In PHP, you can do this with the
strip_tags()
function. - Encode all characters, so if the user types in
<b>hello</b>
, it is displayed as <b>hello</b>
. In PHP, this is htmlspecialchars()
. (Note: in this situation, you usually save the content in the database as is, and use htmlspecialchars wherever you output the content.) - Use a sanitizer for HTML, such as an HTML cleaner . This allows users to use certain HTML formatting, such as bold / italics, but blocks malicious Javascript and any other tags that you want (i.e.
<object>
in your case).
DisgruntledGoat
source share