mysql_real_escape_string() 用于整个 $_REQUEST 数组,还是需要循环遍历它?
除了以下方法之外,还有更简单的方法可以安全地提取提交的变量吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一次性转义所有变量:将
所有变量提取到当前命名空间中(即
$foo = $_GET['foo']
):请不要最后执行此操作尽管迈出一步。没有必要,只需将值保留在数组中即可。提取变量可能会导致名称冲突和覆盖现有变量,这不仅是麻烦和错误来源,而且还存在安全风险。另外,正如 @BoltClock 所说,坚持使用
$_GET
或$_POST
。另外,正如 @zerkms 指出的那样,不应该在数据库查询中使用的 mysql_real_escaping 变量是没有意义的,它甚至可能会导致进一步的问题。请注意,实际上这一切都不是一个特别好的主意,您只是在转世 magic_quotes 和 global_vars,而这些在过去都是可怕的 PHP 实践。通过 mysqli 或 PDO 使用带有绑定参数的准备好的语句,并通过
$_GET
或filter_input
使用值。请参阅 http://www.phptherightway.com。To escape all variables in one go:
To extract all variables into the current namespace (i.e.
$foo = $_GET['foo']
):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 inmysql_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
orfilter_input
. See http://www.phptherightway.com.您还可以使用像这样的递归函数来完成该任务
You can also use a recursive function like this to accomplish that
清理或验证任何
INPUT_GET
、INPUT_POST
、INPUT_COOKIE
、INPUT_SERVER
或INPUT_ENV
,您可以使用filter_input_array
— 获取外部变量并可选择过滤它们过滤可以通过回调完成,因此您可以提供
mysql_real_escape_string
。此方法不允许过滤
$_REQUEST
,因为您不应该工作当数据在任何其他超全局中可用时,使用$_REQUEST
。这可能是不安全的。该方法还要求您命名输入键,因此它不是通用的批量过滤。如果您需要通用批量过滤,请使用
array_map
或array_walk
或array_filter
(如本页其他位置所示)。另外,为什么你使用旧的 mysql 扩展而不是 mysqli (i 为改进)扩展。 mysqli 扩展将为您提供对事务、多重查询 和 准备好的语句(无需转义)所有功能都可以使您的数据库代码更加可靠和安全。
To sanitize or validate any
INPUT_GET
,INPUT_POST
,INPUT_COOKIE
,INPUT_SERVER
, orINPUT_ENV
, you can usefilter_input_array
— Gets external variables and optionally filters themFiltering 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
orarray_walk
orarray_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.
就我而言,Starx' 和 Ryan 在 2010 年 11 月 19 日的回答是最好的解决方案,因为我也需要这个。
当您有多个输入字段具有一个名称(例如names[])时,这意味着它们将被保存到$_POST数组内的数组中,您必须使用递归函数,因为mysql_real_escape_string不适用于数组。
所以这是转义 $_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.
如果您使用 mysqli 扩展并且您想转义所有 GET 变量:
If you use mysqli extension and you like to escape all GET variables:
作为替代方案,我可以建议您使用 PHP7 输入过滤器,它提供了快捷方式sql 转义。我本身并不推荐它,但它可以避免创建本地化变量:
可以在 SQL 查询字符串中内联使用,并在您忘记它时给出额外警告:
它在语法上是有问题的,但允许您仅转义那些真正需要的变量它。或者要模拟您的要求,请使用
$_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:
Which can be used inline in SQL query strings, and give an extra warning should you forget it:
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();