Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strip_tags and htmlentities

Tags:

php

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?

like image 231
Somebody Avatar asked Apr 26 '11 09:04

Somebody


1 Answers

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.

like image 119
NikiC Avatar answered Sep 28 '22 08:09

NikiC