如何在自定义函数中使用 extract() 从用户提供的数组数据生成全局范围的值?
我想修改这个函数以摆脱使用全局变量。
function CatchListing() {
$parseform = array('itemnum','msrp','edprice','itemtype','box','box2','box25','box3','box4','box5','box6','box7','itemcolor','link');
foreach ($parseform as $globalName) {
$GLOBALS[$globalName] = mysql_real_escape_string($_POST[$globalName]);
}
}
我被告知使用 array_map & 然后提取,但我不确定如何构建它。
function CatchListing() {
$_POST['listing'] = array_map('mysql_real_escape_string', $_POST);
$nst = extract($_POST['listing']);
}
(顺便说一句,列表是表单名称)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用带有外部输入值(例如 $_GET 和 $_POST)的提取时要非常小心。
您最好手动将值提取为已知值。
从 _GET 或 _POST 中提取内容很容易破坏现有变量。
Be VERY careful about using extract with externally inputted values as from $_GET and $_POST.
you're much better off extracting the values manually to known values.
It's far too easy for an extract from _GET or _POST to clobber existing variables.
有很多话要说,乔纳森有了一个很好的开始。 每当用户有机会使用您的内部数据而您没有检查它们时,就有很大的“机会”(取决于视图..)出现问题。 这是关于如何“也许”到达您想去的地方的另一种方法:
说实话,它仍然与 OOP 而且应该被视为一种过程方法。 就我个人而言,我会使用额外的可重用函数来“清理”输入,因为你永远不知道,是否有一天你想要更改数据库或“转义”函数,然后你确切地知道在哪里寻找可能的更改。 还有一件事:您确定您不知道您必须期望的所有变量的所有可能名称吗? 也许您可以预先确定它们并将它们放入另一个数组中,并使用 in_array 检查每个用户提供的参数。
There are so many things to say and Jonathan makes a very good start. Every time the user has the opportunity to play with your internal data and you don't check them, there is a huge "opportunity" (depends on the view..) that something goes wrong. Here is another approach on how to "maybe" get where you want to go:
To be honest, it still has not really anything to do with OOP and furthermore should be seen as a procedural approach. Personally I would use an additional and reusable function to "sanitize" the input, because you never know, if someday you want to change your database or the "escape" function and then you exactly know where to look for possible changes. Ah one more thing: Are you certain that you don't know all the possible names of all the variables you have to expect? Maybe you can predetermine them and put them in another array and check each user supplied argument with in_array.
要完全摆脱代码中全局变量的使用,并使其整体更好,您可以按照以下方式执行操作:
以下是我认为我将如何修改您的代码以改进它:
要使用此函数,您只需执行
$result = CatchListings($_POST);
。 结果相同,未使用全局变量。不过,有一点需要考虑。 仅将随机填充的数组(即 $_POST)传递给函数并期望它包含特定的键(即 $requiredKeys 数组)可能不是最好的形式。 您可能想要添加逻辑来检查丢失的键,或者在传递后数组之前对其进行处理。
To completely get rid of the usage of globals in your code, and also to make it much better overall, you can do something along these lines:
Here's how I think I would modify your code to improve it:
To use this function, you simply do
$result = CatchListings($_POST);
. Same result, no globals used.There is one thing to consider, though. It may not be best possible form to just pass a randomly filled array (ie. $_POST) to the function, and expect it to contain specific keys (ie. $requiredKeys array). You might want to either add logic to check for missing keys, or process the post array before passing it.