mysql_real_escape_string used when insert into database
htmlentities() used when outputting data into webpage
htmlspecialchars() used when??
strip_tags() used when ??
addslashes() used when ??
htmlspecialchars()使用时??
htmlspecialchars与htmlentities大致相同。区别:字符编码。
两者都编码控制字符,如,&等等用于打开标签等。htmlentities也编码来自其他语言的字符,例如变音符号,欧元符号等。如果你的网站是utf,使用htmlspecialchars(),否则使用htmlentities()。
strip_tags()使用时??
htmlspecialchars /实体编码特殊字符,因此它们被显示但不被解释。 strip_tags删除它们。
在实践中,这取决于你需要做什么。
一个例子…你编码了一个论坛,并给用户一个文本字段,使他们可以发布的东西。恶意的只是试试
pictures of kittens here
如果你不做任何事情,链接将显示,点击链接的受害者会得到很多弹出窗口。
如果你htmlentitiy / htmlspecialchar你的输出,文本将按原样。如果你strip_tag它,它只是删除标签并显示它:
pictures of kittens here
有时你可能想要一个混合,在那里留下一些标签,如< b> (strip_tags可以留下某些标签)。这也是不安全的,所以更好地使用一些完全爆炸的库对xss
addslashes
引用php手册:
Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote (‘), double quote (“), backslash () and NUL (the NULL byte).
An example use of addslashes() is when you’re entering data into a database. For example, to insert the name O’reilly into a database, you will need to escape it. It’s highly recommeneded to use DBMS specific escape function (e.g. mysqli_real_escape_string() for MySQL or pg_escape_string() for PostgreSQL), but if the DBMS you’re using does’t have an escape function and the DBMS uses \ to escape special chars, you can use this function.