mysql_real_escape_string() 用于整个 $_REQUEST 数组,还是需要循环遍历它?

发布于 2024-10-03 07:02:56 字数 340 浏览 3 评论 0原文

除了以下方法之外,还有更简单的方法可以安全地提取提交的变量吗?

if(isset($_REQUEST['kkld'])) $kkld=mysql_real_escape_string($_REQUEST['kkld']);
if(isset($_REQUEST['info'])) $info=mysql_real_escape_string($_REQUEST['info']);
if(isset($_REQUEST['freq'])) $freq=mysql_real_escape_string($_REQUEST['freq']);

(并且:您会在这种情况下使用 isset() 吗?)

Is there an easier way of safely extracting submitted variables other than the following?

if(isset($_REQUEST['kkld'])) $kkld=mysql_real_escape_string($_REQUEST['kkld']);
if(isset($_REQUEST['info'])) $info=mysql_real_escape_string($_REQUEST['info']);
if(isset($_REQUEST['freq'])) $freq=mysql_real_escape_string($_REQUEST['freq']);

(And: would you use isset() in this context?)

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

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

发布评论

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

评论(6

靑春怀旧 2024-10-10 07:02:56

一次性转义所有变量:将

$escapedGet = array_map('mysql_real_escape_string', $_GET);

所有变量提取到当前命名空间中(即$foo = $_GET['foo']):

extract($escapedGet);

请不要最后执行此操作尽管迈出一步。没有必要,只需将值保留在数组中即可。提取变量可能会导致名称冲突和覆盖现有变量,这不仅是麻烦和错误来源,而且还存在安全风险。另外,正如 @BoltClock 所说,坚持使用 $_GET$_POST。另外,正如 @zerkms 指出的那样,不应该在数据库查询中使用的 mysql_real_escaping 变量是没有意义的,它甚至可能会导致进一步的问题。


请注意,实际上这一切都不是一个特别好的主意,您只是在转世 magic_quotes 和 global_vars,而这些在过去都是可怕的 PHP 实践。通过 mysqli 或 PDO 使用带有绑定参数的准备好的语句,并通过 $_GETfilter_input 使用值。请参阅 http://www.phptherightway.com

To escape all variables in one go:

$escapedGet = array_map('mysql_real_escape_string', $_GET);

To extract all variables into the current namespace (i.e. $foo = $_GET['foo']):

extract($escapedGet);

Please do not do this last step though. There's no need to, just leave the values in an array. Extracting variables can lead to name clashes and overwriting of existing variables, which is not only a hassle and a source of bugs but also a security risk. Also, as @BoltClock says, stick to $_GET or $_POST. Also2, as @zerkms points out, there's no point in mysql_real_escaping variables that are not supposed to be used in a database query, it may even lead to further problems.


Note that really none of this is a particularly good idea at all, you're just reincarnating magic_quotes and global_vars, which were horrible PHP practices from ages past. Use prepared statements with bound parameters via mysqli or PDO and use values through $_GET or filter_input. See http://www.phptherightway.com.

浅沫记忆 2024-10-10 07:02:56

您还可以使用像这样的递归函数来完成该任务

function sanitate($array) {
   foreach($array as $key=>$value) {
      if(is_array($value)) { sanitate($value); }
      else { $array[$key] = mysql_real_escape_string($value); }
   }
   return $array;
}
sanitate($_POST);

You can also use a recursive function like this to accomplish that

function sanitate($array) {
   foreach($array as $key=>$value) {
      if(is_array($value)) { sanitate($value); }
      else { $array[$key] = mysql_real_escape_string($value); }
   }
   return $array;
}
sanitate($_POST);
尬尬 2024-10-10 07:02:56

清理或验证任何 INPUT_GETINPUT_POSTINPUT_COOKIEINPUT_SERVERINPUT_ENV ,您可以使用

过滤可以通过回调完成,因此您可以提供mysql_real_escape_string

此方法不允许过滤 $_REQUEST,因为您不应该工作当数据在任何其他超全局中可用时,使用 $_REQUEST 。这可能是不安全的。

该方法还要求您命名输入键,因此它不是通用的批量过滤。如果您需要通用批量过滤,请使用 array_maparray_walkarray_filter(如本页其他位置所示)。

另外,为什么你使用旧的 mysql 扩展而不是 mysqli (i 为改进)扩展。 mysqli 扩展将为您提供对事务多重查询准备好的语句(无需转义)所有功能都可以使您的数据库代码更加可靠和安全。

To sanitize or validate any INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV, you can use

Filtering can be done with a callback, so you could supply mysql_real_escape_string.

This method does not allow filtering for $_REQUEST, because you should not work with $_REQUEST when the data is available in any of the other superglobals. It's potentially insecure.

The method also requires you to name the input keys, so it's not a generic batch filtering. If you want generic batch filtering, use array_map or array_walk or array_filter as shown elsewhere on this page.

Also, why are you using the old mysql extension instead of the mysqli (i for improved) extension. The mysqli extension will give you support for transactions, multiqueries and prepared statements (which eliminates the need for escaping) All features that can make your DB code much more reliable and secure.

攀登最高峰 2024-10-10 07:02:56

就我而言,Starx' 和 Ryan 在 2010 年 11 月 19 日的回答是最好的解决方案,因为我也需要这个。

当您有多个输入字段具有一个名称(例如names[])时,这意味着它们将被保存到$_POST数组内的数组中,您必须使用递归函数,因为mysql_real_escape_string不适用于数组。

所以这是转义 $_POST 变量的唯一解决方案。

function sanitate($array) {
    foreach($array as $key=>$value) {
        if(is_array($value)) { sanitate($value); }
            else { $array[$key] = mysql_real_escape_string($value); }
   }
   return $array;
}
sanitate($_POST);

As far as I'm concerned Starx' and Ryan's answer from Nov 19 '10 is the best solution here as I just needed this, too.

When you have multiple input fields with one name (e.g. names[]), meaning they will be saved into an array within the $_POST-array, you have to use a recursive function, as mysql_real_escape_string does not work for arrays.

So this is the only solution to escape such a $_POST variable.

function sanitate($array) {
    foreach($array as $key=>$value) {
        if(is_array($value)) { sanitate($value); }
            else { $array[$key] = mysql_real_escape_string($value); }
   }
   return $array;
}
sanitate($_POST);
等数载,海棠开 2024-10-10 07:02:56

如果您使用 mysqli 扩展并且您想转义所有 GET 变量:

$escaped_get = array_map(array($mysqli, 'real_escape_string'), $_GET);

If you use mysqli extension and you like to escape all GET variables:

$escaped_get = array_map(array($mysqli, 'real_escape_string'), $_GET);
围归者 2024-10-10 07:02:56

作为替代方案,我可以建议您使用 PHP7 输入过滤器,它提供了快捷方式sql 转义。我本身并不推荐它,但它可以避免创建本地化变量:

 $_REQUEST->sql['kkld']

可以在 SQL 查询字符串中内联使用,并在您忘记它时给出额外警告:

 mysql_query("SELECT x FROM y WHERE z = '{$_REQUEST->sql['kkld']}'");

它在语法上是有问题的,但允许您仅转义那些真正需要的变量它。或者要模拟您的要求,请使用 $_REQUEST->sql->always();

As an alternative, I can advise you to use PHP7 input filters, which provides a shortcut to sql escaping. I'd not recommend it per se, but it spares creating localized variables:

 $_REQUEST->sql['kkld']

Which can be used inline in SQL query strings, and give an extra warning should you forget it:

 mysql_query("SELECT x FROM y WHERE z = '{$_REQUEST->sql['kkld']}'");

It's syntactically questionable, but allows you escaping only those variables that really need it. Or to emulate what you asked for, use $_REQUEST->sql->always();

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