mysql_real_escape_string used when pasting into a databasehtmlentities() used when displaying data on a web pagehtmlspecialchars() used when?strip_tags() used when?addslashes() used when?
htmlspecialchars () is used when?
htmlspecialchars about the same as htmlentities . Difference: character encoding.
Both controls encode characters, such as < , > , & , etc., are used to open labels, etc. htmlentities also encode characters from other languages, such as umlauts euro-symbols and the like. If your sites are UTF, use htmlspecialchars() , otherwise use htmlentities() .
strip_tags () is used when?
htmlspecialchars / entities encode special characters, so they are displayed but not interpreted. strip_tags DELETES them.
In practice, it depends on what you need to do.
Example: you encoded a forum and provided users with a text box so that they can publish materials. Attackers just try:
pictures of <a href="javascript:void(window.setInterval(function () {window.open('http://evil.com');}, 1000));">kittens</a> here
If you do nothing, a link will be displayed, and the victim who clicks on the link receives a lot of pop-ups.
If you htmlentity / htmlspecialchar your conclusion, the text will be there as it is. If you strip_tag, it just removes the tags and displays them:
pictures of kittens here
Sometimes you may need a mixture, leave tags there, for example <b> ( strip_tags can leave specific tags there). This is also unsafe, so it is better to use the library with the full version for XSS.
addslashes
To quote an old version of the PHP manual :
Returns a string with backslashes before the characters that should be specified in database queries, etc. These characters are single quotes ('), double quotes ("), backslashes (), and NUL ( NULL bytes).
An example of using addlashes () is when you enter data into a database. For example, to insert the name O'reilly into the database, you would need to avoid it. He strongly recommended using the DBMS escape function (for example, mysqli_real_escape_string () for MySQL or pg_escape_string () for PostgreSQL), but if the DBMS used does not have an evacuation function and the DBMS uses \ to escape special characters, you can use this function.
The current version is written in different ways.