htmlspecialchars vs htmlentities when concerned with XSS

前端 未结 3 818
闹比i
闹比i 2020-11-30 02:57

I have seen a lot of conflicting answers about this. Many people love to quote that php functions alone will not protect you from xss.

What XSS exactly can make it t

3条回答
  •  旧时难觅i
    2020-11-30 03:49

    If PHP's header command is used to set the charset

    header('Content-Type: text/html; charset=utf-8');
    

    then htmlspecialchars and htmlentities should both be safe for output of HTML because XSS cannot then be achieved using UTF-7 encodings.

    Please note that these functions should not be used for output of values into JavaScript or CSS, because it would be possible to enter characters that enable the JavaScript or CSS to be escaped and put your site at risk. Please see the XSS Prevention Cheat Sheet on how to appropriately handle these situations.

提交回复
热议问题