Should I use htmlentities
with strip_tags
?
I am currently using strip_tags
when adding to database and thinking about removing htmlentities
on output; I want to avoid unnecessary processing while generating HTML on the server.
Is it safe to use only strip_tags
without allowed tags?
First: Use the escaping method only as soon as you need it. I.e. if you insert something into a database, only escape it for the database, i.e. apply mysql_real_escape_string
(or PDO->quote
or whatever database layer you are using). But don't yet apply any escaping for the output. No strip_tags
or similar yet. This is because you may want to use the data stored in the database someplace else, where HTML escaping isn't necessary, but only makes the text ugly.
Second: You should not use strip_tags
. It removes the tags altogether. I.e. the user doesn't get the same output as he typed in. Instead use htmlspecialchars
. It will give the user the same output, but will make it harmless.
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