I am trying to come up with a function that I can pass all my strings through to sanitize. So that the string that comes out of it will be safe for database insertion. But t
For database insertion, all you need is mysql_real_escape_string
(or use parameterized queries). You generally don't want to alter data before saving it, which is what would happen if you used htmlentities
. That would lead to a garbled mess later on when you ran it through htmlentities
again to display it somewhere on a webpage.
Use htmlentities
when you are displaying the data on a webpage somewhere.
Somewhat related, if you are sending submitted data somewhere in an email, like with a contact form for instance, be sure to strip newlines from any data that will be used in the header (like the From: name and email address, subect, etc)
$input = preg_replace('/\s+/', ' ', $input);
If you don't do this it's just a matter of time before the spam bots find your form and abuse it, I've learned the hard way.