htmlentities
(or htmlspecialchars
) and addslashes
(or mysql_real_escape_string
or other database-appropriate escaping function; addslashes is invariably the wrong thing) are nothing to do with GET/POST parameters. They are outgoing-escaping functions whose input might come from parameters, but might equally come from somewhere else, such as a static string or a string fetched from the database.
for now i'm using them both on each get/post parameter.
No, that doesn't work. It's a common mistake, and one perpetuated by a lot of the totally dreadful PHP tutorial material out there. Trying to split off the problem of string escaping into one little loop instead of being spread all over the code sounds nice, but string escaping doesn't work like that in reality. You need to apply the right form of escaping, and only the right form of escaping, at each time you put a string inside another string. You cannot do that in advance.
Don't try to ‘sanitise’ or ‘filter’ all the incoming parameters to encode or remove characters like \
or <
. This will result in your application filling up with rubbish like \\\\\\\\\\\\\\\\\\\\\\\\\\
and &amp;amp;amp;amp;amp;amp;
. Instead, you should only encode them — along with any other varaibles that don't come from GET/POST — at the last minute when spitting them into a different context of text, like htmlspecialchars
for HTML or mysql_real_escape_string
for MySQL string literals. You are usually better off with parameterised queries though, which avoids the SQL escaping issues.
That's not to say you shouldn't check your incoming data for conformance to your application's requirements. If you've got a field you expect to get an integer for, be sure to intval
it before using it. If you've got a plain single-line text string which you don't expect to contain newlines, be sure to filter the \n
character from the string, along with all other control characters [\x00-\x1F\x7F]
. But these are application requirements, not something you can necessarily run across all your input.