简体   繁体   English

如果我使用以下内容,是否正式使用表单验证?

[英]Is Regex for Form Validation if I use the following?

I know there is no harm in adding it either way but I'm curious... 我知道无论如何添加它都没有害处,但我很好奇......

If I was to use htmlentities(); 如果我要使用htmlentities(); with ENT_QUOTES and then mysql_real_escape_string(); 使用ENT_QUOTES然后使用mysql_real_escape_string(); the variable before entering it into the Database, then just use html_entity_decode(); 在将变量输入数据库之前的变量,然后只需使用html_entity_decode(); along with stripslashes(); 与stripslashes(); to display the information... 显示信息......

Would this still be safe and secure? 这仍然是安全的吗?

You don't need to use htmlentities before storing data in the database. 在将数据存储到数据库之前,您不需要使用htmlentities。 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). 在HTML输出中回显它们时,只对字符串使用htmlentities(无论是从数据库还是从其他来源获取字符串)。

You don't need to apply stripslashes to data after you fetch it from the database. 从数据库中获取数据后,您不需要对数据应用stripslashes。 The database has not stored the extra escaping characters -- unless you applied double-escaping by mistake. 数据库没有存储额外的转义字符 - 除非您错误地应用了双重转义。

Here's the right sequence: 这是正确的顺序:

  1. Get data from a form 从表单中获取数据

     $input = $_GET["input"]; 
  2. Apply escaping once . 申请逃脱一次

     $quoted_input = "'" . mysql_real_escape_string($input) . "'"; 
  3. Insert it into the database 将其插入数据库

     $sql = "INSERT INTO MyTable (column1) VALUES ($quoted_input)"; $success = mysql_query($sql); 
  4. Later fetch it from the database 稍后从数据库中获取它

     $sql = "SELECT column1 FROM MyTable"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); $data = $row["column1"]; 
  5. Apply htmlentities once as you output. 输出时应用htmlentities 一次

     echo htmlentities($data); 

Maybe you can answer the question on your own if you know what these functions are intended to be used for: 如果您知道这些函数的用途,您可以自己回答这个问题:

If you just want to protect you from SQL injections, use mysql_real_escape_string for data that is used in MySQL queries. 如果您只想保护自己免受SQL注入,请将mysql_real_escape_string用于MySQL查询中使用的数据。 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.). 您还可以使用预准备语句或参数化查询构建器(请参阅准备语句的SQL语法PDO - 准备语句和存储过程MySQLi :: prepare等)。

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. 我刚刚编写了一个验证类,其函数可以在需要特定输入时使用正则表达式清理代码和其他函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM