One thing you should not do is filter the input as it arrives. People often suggest this because this is the easiest solution, but it leads to problems.
Input data can be sent to several places, in addition, they are output as HTML. For example, it may be stored in a database. The rules for filtering data sent to the database are very different from the rules for filtering HTML output. If you encode the HTML in the input, you will get the HTML in your database. (This is also why the PHP function "magic quotes" is a bad idea.)
You cannot foresee all the places where your input will move. A safe approach is to prepare the data immediately before it is sent somewhere. If you send it to the database, avoid single quotes. If you output HTML, avoid HTML objects. And as soon as it goes somewhere, if you still need to work with the data, use the original version without escaping.
This is more, but you can reduce it using template engines or libraries.
JW.
source share