如果我使用以下内容,正则表达式是否用于表单验证?
我知道以任何方式添加它都没有坏处,但我很好奇......
如果我要使用 htmlentities();使用 ENT_QUOTES,然后使用 mysql_real_escape_string();在将变量输入数据库之前,只需使用 html_entity_decode();与 stripslashes() 一起使用;显示信息...
这仍然安全吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在数据库中存储数据之前,您不需要使用 htmlentities。事实上,如果你不这样做,以后的事情会变得更容易。仅当您在 HTML 输出中回显字符串时才对字符串使用 htmlentities(无论您是从数据库还是从其他源获取字符串)。
从数据库获取数据后,无需对数据应用斜杠。数据库没有存储额外的转义字符——除非您错误地应用了双重转义。
以下是正确的顺序:
从表单获取数据
应用转义一次。
插入数据库
稍后从数据库中获取
在输出时应用 htmlentities一次。
You don't need to use htmlentities before storing data in the database. In fact, it makes things easier later if you don't. Only use htmlentities on strings as you echo them in HTML output (whether you fetched the string from a database or from some other source).
You don't need to apply stripslashes to data after you fetch it from the database. The database has not stored the extra escaping characters -- unless you applied double-escaping by mistake.
Here's the right sequence:
Get data from a form
Apply escaping once.
Insert it into the database
Later fetch it from the database
Apply htmlentities once as you output.
如果您知道这些函数的用途,也许您可以自己回答这个问题:
htmlentities
是替换HTML特殊字符&
、<
、> 和
"
和 可以表示的字符通过实体字符引用,这用于对要安全地放入任何 HTML 上下文中的数据进行编码(特别是使用ENT_QUOTES,以便它甚至可以在单引号属性值中使用)。示例:mysql_real_escape_string
是替换 MySQL 字符串中的特殊字符,同时考虑连接字符编码(使用mysql_client_encoding
是必需的)。这用于对数据进行编码,以便在 MySQL 字符串中安全使用。例如:html_entity_decode
是相反的函数htmlentities
并替换 HTML 字符引用(数字和实体字符引用)。stripslashes
删除了转义字符\
.如果您只是想防止 SQL 注入,请对 MySQL 查询中使用的数据使用 mysql_real_escape_string。您还可以使用准备好的语句或参数化查询生成器(请参阅准备语句的 SQL 语法、PDO – 准备语句和存储过程、 MySQLi::prepare 等)。
Maybe you can answer the question on your own if you know what these functions are intended to be used for:
htmlentities
is to replace the HTML special characters&
,<
,>
, and"
and characters that can be represented by entity character references. This is used to encode data to be safely put out in any HTML context (especially with ENT_QUOTES so that it even can be used in single quoted attribute values). For example:mysql_real_escape_string
is to replace the special characters in a MySQL string while taking the connection character encoding into account (usingmysql_client_encoding
is required). This is used to encode data to be safely used in a MySQL string. For example:html_entity_decode
is the inverse function tohtmlentities
and replaces HTML character references (both numeric and entity character references).stripslashes
removed the escape character\
.If you just want to protect you from SQL injections, use
mysql_real_escape_string
for data that is used in MySQL queries. You could also use prepared statements or parameterized query builder (see SQL Syntax for Prepared Statements, PDO – Prepared Statements und Stored Procedures, MySQLi::prepare, et al.).您是否在问您是否仍然需要正则表达式作为所有这些函数旁边的表单验证?
如果这就是您所问的问题,那么在我看来,是的,您永远都不够安全。我刚刚编写了一个验证类,其中的函数可以在需要特定输入时使用正则表达式清理代码和其他函数。
are you asking if you still need regex as form validation next to all those functions?
if that is what you are asking then in my opinion yes, you can never be safe enough. I've just written a validation class with functions that clean up the code and other functions with regex when I need a specific input.