Should i use 'strip_tags() ' before inserting into database or before printing on page?

Afik Habaz

I am following various steps to prevent any kind of injections or attacks on my web application. While I was following them a question came to my head: One of the very basic actions to take to prevent attacks on your web app is to prevent the user from writing malicious scripts that could possibly hurt the other users. In the sources I've read it said that you should use strip_tags() or htmlentities() but it wasn't clear enough if I should put it before I am inserting the user data into the database or before I am printing it out or maybe both..

So basically my question is when do I use these functions? And also what if I want to keep some of the html tags?{In case I am using a html text editor} and is it recommended at all?

Thanks in advance.

Dharman

strip_tags() is used to sanitize the data you receive from the users. It will try to remove all NULL bytes, HTML and PHP tags. Personally I am not a fan of this function as there are better ways to validate and sanitize input, e.g filter-input.
If you would like to keep some HTML you could take a look at http://htmlpurifier.org/

htmlentities() is used to output data into HTML context. If you are displaying anything in HTML (doesn't matter where the data comes from) you should use this to prevent XSS. More information

To prevent SQL injections you should use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. If you make sure to never mix data and SQL you can insert any data you would like to DB, even other SQL.

They all have different uses and prevent different kind of bugs.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Should I use "hasClass" before "addClass"?

分類Dev

What should be done with parsed text before inserting it to DB by SQL?

分類Dev

How can I use :before property to create a square before a span

分類Dev

SQLite: should I detach attached databases before a disconnect?

分類Dev

Should I change app IDs before re-launching

分類Dev

Should I check object existence before selecting with jQuery?

分類Dev

Should I do git fetch before pull if branch exists locally?

分類Dev

Should I learn object-c to before learning swift?

分類Dev

Is there a way to check for errors first from inserting in two related rows before saving in database

分類Dev

PHP - Check if row exists before inserting

分類Dev

Filter duplicate nodes before inserting into XML field

分類Dev

Go to another activity before inserting from gallery

分類Dev

Should .cloned() be before or after .filter()

分類Dev

Python console not printing before subprocess is called

分類Dev

How can I use SonarQube before making a commit?

分類Dev

How can I use HTML elements in js before declaring it?

分類Dev

how get page before? Xamarin

分類Dev

Weird issue date printing correctly in php but inserting into database as -2017

分類Dev

Should I change Admob test id before submitting to Apple Certification Team for review? SwiftUI

分類Dev

Should I add sudo before the fastboot command on macOS just like on Linux?

分類Dev

What datatype should I use for saving a file in a SQL Azure database?

分類Dev

which database should I use for simple log in register form in android

分類Dev

How to filter an html ajax response's elements before inserting into document?

分類Dev

Inserting in a multiset: before the first occurence of that value instead of after the last occurence

分類Dev

Validating user input before inserting in a vector C++

分類Dev

Inserting the filename before the first line of a text file - File extension

分類Dev

Inserting user provided dates before 1970 to MySql using Laravel and Carbon

分類Dev

ESLintno-use-before-define

分類Dev

Popup appear on wrong position if i scroll page first before clicking on corresponding link

Related 関連記事

  1. 1

    Should I use "hasClass" before "addClass"?

  2. 2

    What should be done with parsed text before inserting it to DB by SQL?

  3. 3

    How can I use :before property to create a square before a span

  4. 4

    SQLite: should I detach attached databases before a disconnect?

  5. 5

    Should I change app IDs before re-launching

  6. 6

    Should I check object existence before selecting with jQuery?

  7. 7

    Should I do git fetch before pull if branch exists locally?

  8. 8

    Should I learn object-c to before learning swift?

  9. 9

    Is there a way to check for errors first from inserting in two related rows before saving in database

  10. 10

    PHP - Check if row exists before inserting

  11. 11

    Filter duplicate nodes before inserting into XML field

  12. 12

    Go to another activity before inserting from gallery

  13. 13

    Should .cloned() be before or after .filter()

  14. 14

    Python console not printing before subprocess is called

  15. 15

    How can I use SonarQube before making a commit?

  16. 16

    How can I use HTML elements in js before declaring it?

  17. 17

    how get page before? Xamarin

  18. 18

    Weird issue date printing correctly in php but inserting into database as -2017

  19. 19

    Should I change Admob test id before submitting to Apple Certification Team for review? SwiftUI

  20. 20

    Should I add sudo before the fastboot command on macOS just like on Linux?

  21. 21

    What datatype should I use for saving a file in a SQL Azure database?

  22. 22

    which database should I use for simple log in register form in android

  23. 23

    How to filter an html ajax response's elements before inserting into document?

  24. 24

    Inserting in a multiset: before the first occurence of that value instead of after the last occurence

  25. 25

    Validating user input before inserting in a vector C++

  26. 26

    Inserting the filename before the first line of a text file - File extension

  27. 27

    Inserting user provided dates before 1970 to MySql using Laravel and Carbon

  28. 28

    ESLintno-use-before-define

  29. 29

    Popup appear on wrong position if i scroll page first before clicking on corresponding link

ホットタグ

アーカイブ