addslashes
is irrelevant to XSS (and there is almost always something better in places where it is actually useful).
htmlspecialchars
is not an unsafe approach. It is just insufficient by itself.
htmlspecialchars
will protect you if you put the content as the body of a "safe" element.
It will protect you if you put the content as the value of a "safe" attribute if you also properly quote the value.
It won't protect you if you put it as the value of an "unsafe" attribute or element (where the content may be treated as JavaScript) such as <script>
, onmoseover
, href
or style
.
For example:
<!-- http://example.com/my.php?message=", steal_your_cookies(), " -->
<!-- URL not encoded for clarity. Imagine the definition of steel_your_cookies was there too -->
<button onclick='alert("<?php echo htmlspecialchars($_GET['message']); ?>")'>
click me
</button>
will give you:
<button onclick='alert("", steal_your_cookies(), "")'>
click me
</button>
which means the same as:
<button onclick='alert("", steal_your_cookies(), "")'>
click me
</button>
which will steal your cookies when you click the button.