Using htmlspecialchars
is sufficient when inserting inside HTML code. The way it encodes the characters makes it impossible for the resulting text to “break out” of the current element. That way it can neither create other elements, nor script segments etc.
However in all other situations, htmlspecialchars
it not automatically enough. For example when you use it to insert code within some JavaScript area, for example when you fill a JavaScript string with it, you will need additional methods to make it safe. In that case addslashes
could help.
So depending on where you insert the resulting text, htmlspecialchars
gives you either enough security or not. As the function name already suggests, it just promises security for HTML.
<script>alert('test')</script>
as a username, but I don't want that javascript to get executed when another user visits his profile. – Wohlert