Efficiently sanitize user entered text

前端 未结 3 1173
無奈伤痛
無奈伤痛 2020-12-09 06:28

I have a html form that accepts user entered text of size about 1000, and is submitted to a php page where it will be stored in mysql database. I use PDO with prepared state

3条回答
  •  清歌不尽
    2020-12-09 07:01

    You need to distinguish between two types of attacks: SQL injection and XSS. SQL injection can be avoided by using prepared statements or the quote functions of your DB library. You use the quoting function this before inserting into the database.

    XSS can be avoided by quoting all special chars with htmlspecialchars. It is considered good style to escape the output after you read it from the database and store the original input in the database. This way, when you use the input in other contexts where HTML escaping is not needed (text email, JSON encoded string) you still have the original input form the user.

    Also see this answer to a similar question.

提交回复
热议问题