I have a user input field which will be stored into a ‘tinytext’ field in a MySQL database; pretty standard stuff. I am wondering if there is some sort of standard or best-practice to adhere to when it comes to escaping html special characters using the php function htmlentities()?
Should I use htmlentities() before I store the data in the database or should I run the function on the data ever time it is output from the website?
There is usually no reason to use
htmlentities()at all any more. Just store everything in UTF-8 fields and adhere to UTF-8 all the way through.When outputting unsafe user input as HTML, use
htmlspecialchars(), ideally at the time of output so you have a copy of the original data.