The measures taken to secure user input depends entirely on in what context the data is being used. For instance:
- If you're inserting it into a SQL database, you should use parameterized statements. PHP's
mysql_real_escape_string()
works decently, as well.
- If you're going to display it on an HTML page, then you need to strip or escape HTML tags.
- In general, any time you're mixing user input with another form of mark-up or another language, that language's elements need to be escaped or stripped from the input before put into that context.
The last point above segues into the next point: Many feel that the original input should always be maintained. This makes a lot of sense when, later, you decide to use the data in a different way and, for instance, HTML tags aren't a big deal in the new context. Also, if your site is in some way compromised, you have a record of the exact input given.
Specifically related to HTML tags in user input intended for display on an HTML page: If there is any conceivable reason for a user to input HTML tags, then simply escape them. If not, strip them before display.