Prevent XSS with strip_tags()?

I strongly disagree it’s “academically better”.

  • It breaks user input (imagine how useless StackOverflow would be for this discussion if they “cleaned” posts from all tags).

  • Text inserted in HTML with only tags stripped will be invalid. HTML requires & to be escaped as well.

  • It’s not even safe in HTML! strip_tags() is not enough to protect values in attributes, e.g., <input value="$foo"> might be exploited with $foo = " onfocus="evil() (no <,> needed!)

So the correct solution is to escape data according to requirements of language you’re generating. When you have plain text and you’re generating HTML, you should convert text to HTML with htmlspecialchars() or such. When you’re generating e-mail, you should convert text to quoted-printable format, and so on.

Leave a Comment