Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use both striptags() and htmlspecialchars() to prevent XSS?

Does this depend on if the input is going to be printed to the user? In my case I need to return the input back to the user (comments and bio).

Thanks!!!

like image 590
KRB Avatar asked Aug 29 '11 16:08

KRB


People also ask

Does Htmlspecialchars prevent XSS?

Use the PHP htmlspecialchars() function to convert special characters to HTML entities. Always escape a string before displaying it on a webpage using the htmlspecialchars() function to prevent XSS attacks.

What is strip_ tags function in PHP?

The strip_tags() function is an inbuilt function in PHP which is used to strips a string from HTML, and PHP tags. This function returns a string with all NULL bytes, HTML, and PHP tags stripped from a given $str. Syntax: string strip_tags( $str, $allowable_tags )


1 Answers

htmlspecialchars() is enough to prevent XSS.

Strip tags removes tags but not special characters like " or ', so if you use strip_tags() you also have to use htmlspecialchars().

If you want users' comments to be displayed like they typed them, don't use strip_tags, use htmlspecialchars() only.

like image 150
Arnaud Le Blanc Avatar answered Oct 27 '22 00:10

Arnaud Le Blanc