I am a little confused on this. I have been reading about htmlspecialchars()
and I am planning to use this for the textareas POST
to prevent XSS attack. I understand that usually htmlspecialchars()
are used to generate the HTML output that is sent to the browser. But what I am not sure is:
1) Is it a safe practice to use htmlspecialchars()
to the user input data before I insert it into MySQL? I am already using PDO prepared statement with parameterized values to prevent SQL Injection.
2) Or, I really dont need to worry about using htmlspecialchars()
to inserted values (provided they are parameterized) and only use htmlspecialchars()
when I fetch results from MySQL and display it to users?
<a href='test'>Test</a>
. When this is escaped during inserting and escaped again during output, will it then it show as<a href='test'>Test</a>
instead of showing it as<a href='test'>Test</a>
in the browser? – Laity<a href='test'>Test</a>
with the intent of transforming it into a real working link, you would have to decode it before output. If you wanted it to show literally as HTML markup, one encoding would display in the browser as<a href='test'>Test</a>
and double-encoding would display as<a href='test'>Test</a>
and neither would get you a working link. – Freestandinghtmlspecialchars()
at the end & make sure I make it a habit to encode it everytime I display any fetched results to the end user. This practice will perhaps make it a good habit. I was confused about this since most articles I read about XSS said that I need to be validate & Sanitize all user inputs. So I wasnt sure what to do with textarea when I dont have anything to validate that with such as expecting a set numbers or set values, etc before insert. So all of your answers have given me a clearer understanding. :) – Laity