I am creating a forum software using php and mysql backend, and want to know what is the most secure way to escape user input for forum posts.
I know about htmlentities() and strip_tags() and htmlspecialchars() and mysql_real_escape_string(), and even javascript’s escape() but I don’t know which to use and where.
What would be the safest way to process these three different types of input (by process, I mean get, save in a database, and display):
- A title of a post (which will also be the basis of the URL permalink).
- The content of a forum post limited to basic text input.
- The content of a forum post which allows html.
I would appreciate an answer that tells me how many of these escape functions I need to use in combination and why.
Thanks!
When generating HTLM output (like you’re doing to get data into the form’s fields when someone is trying to edit a post, or if you need to re-display the form because the user forgot one field, for instance), you’d probably use
htmlspecialchars(): it will escape<,>,",', and&— depending on the options you give it.strip_tagswill remove tags if user has entered some — and you generally don’t want something the user typed to just disappear 😉At least, not for the “content” field 🙂
Once you’ve got what the user did input in the form (ie, when the form has been submitted), you need to escape it before sending it to the DB.
That’s where functions like
mysqli_real_escape_stringbecome useful : they escape data for SQLYou might also want to take a look at prepared statements, which might help you a bit 😉
with mysqli – and with PDO
You should not use anything like
addslashes: the escaping it does doesn’t depend on the Database engine ; it is better/safer to use a function that fits the engine (MySQL, PostGreSQL, …) you are working with : it’ll know precisely what to escape, and how.Finally, to display the data inside a page :
htmlspecialchars(): if the user did input HTML tags, those will be displayed as-is, and not injected as HTML.strip_tags(which can do that) is not really up to the task (it will let attributes of the allowed tags)Those are only a few pointers… hope they help you 🙂
Don’t hesitate to ask if you have more precise questions !