When you're going to output something, strip_tags
or htmlspecialchars
is fine. I prefer the latter, since you then don't totally destroy <3
and the like that were never meant as HTML tags anyway.
When putting a value directly into a query, mysql_real_escape_string
is the appropriate way to escape, or just using PDO and prepared statements.
Of course, it's best to do these escape methods only when you're about to need them, rather than applying both to all variables ever. It's always a pain to have to strip the slashes back out of a MySQL-escaped variable if you want to actually work with it before putting it into the database, and that's no better than PHP4's magic quotes. Similarly, you don't want to turn someone's password of "one<two" into "one<two" before inserting it into the database. (Obviously, you shouldn't be storing plaintext passwords, but the generic example stands.)