phpsecurityhtmlspecialcharsaddslashes

htmlspecialchars + addslashes, makes any sense?


I'm trying to sanitize a string to be saved in a db. First step I took was to use addslashes(), but then I realized it didn't solve many security issues, so I added htmlspecialchars(), and now I have this line of code:

$val=htmlspecialchars(addslashes(trim($val)));

But then I was wondering if it makes any sense at all to use addslashes() on a string that will be processed by htmlspecialchars(), since the latter will "remove" any element that would cause problems, if I'm not mistaken.

In particular, I was wondering if that makes the server work twice without any real need.


Solution

  • I wouldn't use either of those when saving the string to the database.