I've read that it's enough and even recommended to escape characters on the output, not on the input.
It could be easily applied to all get variables as they are not injected to the database from the form level.
However, I'm not sure what to do with all post variables. If it doesn't come from the database, so if it's a raw input data, escaping is fully needed. But I'm using PDO prepare/execute to escape all variables. Questions now:
- Is it OK to use PDO's prepare/execute both in select and insert statements? Isn't it escaping variables twice ?
- Let's say I get some variable through PDO exeute statement - is it ok to display this variable simply with $_POST['variable'] without escaping it (if it was already done in PDO function) ?
- Is htmlspecialchars() enough to escape things such as GET variables which don't come from the database?
And the most important - is all of this, PDO prepare/execute, and htmlspecialchars(), enough to prevent all XSS attacks? Or should I also do more? If so, what should this be? Removing all html tags from the input? Using BB-Code instead?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…