魔术引号和 mysql_real_escape_string

发布于 2024-12-14 17:38:10 字数 1684 浏览 0 评论 0原文

这是原始代码:

       if (($handle = fopen($source_file, "r")) !== FALSE) {
    $columns = fgetcsv($handle, $max_line_length, ",");
    foreach ($columns as &$column) {
        $column = str_replace(".","",$column);
    }
    while (($data = fgetcsv($handle, $max_line_length, ",")) !== FALSE) {
        while(count($data) < count($columns)) {
            array_push($data, NULL);
        }
        $c = count($data);
        for($i = 0; $i < $c; $i++) {
            $data[$i] = "'{$data[$i]}'";
        }

        $sql[] = '(' . implode(',', $data) . ", '" . $_POST['custgroup'] . "'," . $_POST['user_id'] . ')';
    }


$query = "INSERT INTO $target_table (" . implode(',', $columns) . 
      ',custgroup,user_id) VALUES ' . implode(',', $sql);


    //mysql_query($query) or trigger_error(mysql_error());
    echo $query;
    fclose($handle);
}

但是一旦我添加了 mysql_real_escape_string:

   $query = "INSERT INTO $target_table (" . implode(',',array_map('mysql_real_escape_string', $columns)) . 
          ',custgroup,user_id) VALUES ' . implode(',',array_map('mysql_real_escape_string', $sql));

查询将变成:

INSERT INTO UserAddedRecord (lastname,firstname,ceLL,fax,email,code,custgroup,user_id) VALUES (\'Last\',\'首先\',\'01122331\',\'\',\'[电子邮件受保护]\',\'12345\', \'\', 17)

我检查了我的 php.ini 并get_magic_quotes_gpc(),魔术报价被禁用。

magic_quotes_gpc = 关

magic_quotes_runtime = 关

magic_quotes_sybase = 关

应该是什么问题?或者我应该只应用 stripslashes()?但我假设只有在启用魔术引用时才会使用它。

Here is the original code:

       if (($handle = fopen($source_file, "r")) !== FALSE) {
    $columns = fgetcsv($handle, $max_line_length, ",");
    foreach ($columns as &$column) {
        $column = str_replace(".","",$column);
    }
    while (($data = fgetcsv($handle, $max_line_length, ",")) !== FALSE) {
        while(count($data) < count($columns)) {
            array_push($data, NULL);
        }
        $c = count($data);
        for($i = 0; $i < $c; $i++) {
            $data[$i] = "'{$data[$i]}'";
        }

        $sql[] = '(' . implode(',', $data) . ", '" . $_POST['custgroup'] . "'," . $_POST['user_id'] . ')';
    }


$query = "INSERT INTO $target_table (" . implode(',', $columns) . 
      ',custgroup,user_id) VALUES ' . implode(',', $sql);


    //mysql_query($query) or trigger_error(mysql_error());
    echo $query;
    fclose($handle);
}

But once I added mysql_real_escape_string:

   $query = "INSERT INTO $target_table (" . implode(',',array_map('mysql_real_escape_string', $columns)) . 
          ',custgroup,user_id) VALUES ' . implode(',',array_map('mysql_real_escape_string', $sql));

The query will become :

INSERT INTO UserAddedRecord (lastname,firstname,ceLL,fax,email,code,custgroup,user_id) VALUES (\'Last\',\'First\',\'01122331\',\'\',\'[email protected]\',\'12345\', \'\',17)

I checked my php.ini and get_magic_quotes_gpc(),magic quote is disabled.

magic_quotes_gpc = Off

magic_quotes_runtime = Off

magic_quotes_sybase = Off

What should be the problem?or I should just apply stripslashes()?But I assume it will be used only when magic quote is enabled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

未蓝澄海的烟 2024-12-21 17:38:15

您应该遵循以下原则:

  • 所有字符串值使用 mysql_real_escape_string
  • 将所有 interters 转换为 (int)
  • after 应用周围的单引号 ( ' )

对 mysql_real_escape_string 是转义测试内容中的特殊字符,因此使用 stripslashes 会将其删除。

解决这个问题的另一种方法是查看 preparedStatements您不需要向客户端转义。

请记住,这样做的全部目的是为了防止SQL注入并且采取相应的行动:)

You should to something along the lines of:

  • use mysql_real_escape_string on all string values
  • cast all the interters to (int)
  • after that apply the surrounding single quotes ( ' )

The point of mysql_real_escape_string is to escape the special chars within your test content so using stripslashes would take that away.

Another way to solve that is to look into preparedStatements where you don't need to to client side esacping.

Just remember that the whole point of doing this is to prevent SQL-Injection and act accordingly :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文