从超全局中删除数组($_GET、$_POST...)

发布于 2024-12-17 05:19:06 字数 1158 浏览 0 评论 0原文

在 PHP 中,可以在 $_GET$_POST$_COOKIE$_FILES中包含数组>$_REQUEST 根据 PHP 文档 。问题是这些来自用户,我可能会得到它们而不是字符串。例如,考虑以下代码片段。

<?php
if (isset($_GET['hello'])) {
    echo 'Hello, ', htmlspecialchars($_GET['hello']), '.';
}
else {
    echo '<form action="?"><input name="hello"><input type="submit"></form>';
}

看起来还好吗?好吧,只要你不使用 URL,它就会正常工作。问题是黑客可以尝试将 $_GET['hello'] 制作为数组。如果 URL 字符串看起来像 ?hello[]=something PHP 将返回错误消息

警告:htmlspecialchars() 期望参数 1 为字符串,第 3 行 FILE 中给出的数组

OK,谁会在生产站点中启用 HTML 错误(另一件事是错误日志...)。但整数也是一个问题 - PHP 是动态类型语言,因此它可以轻松接受像 '2' + 2 这样的代码。虽然是的,但您可以使用 (int) 我有一些旧代码无法执行此操作。如果字符串来自 $_GET$_POST,它也可能是数组。像 array('2') + 2 这样的东西会导致 PHP 的致命错误。

致命错误:第 3 行 FILE 中不支持的操作数类型

现在这是不可接受的,因为它会停止脚本。我不需要这些变量中的数组,但它们让我烦恼。是否有任何简单的片段可以从这些特殊变量中删除数组。如果我真的想要一个数组,我可以在运行代码片段之前复制它。

In PHP it's possible to have arrays in $_GET, $_POST, $_COOKIE, $_FILES and $_REQUEST according to PHP documentation. The problem is that those come from user and I might get them instead of strings. For example, consider following snippet.

<?php
if (isset($_GET['hello'])) {
    echo 'Hello, ', htmlspecialchars($_GET['hello']), '.';
}
else {
    echo '<form action="?"><input name="hello"><input type="submit"></form>';
}

Looks OK? Well, as long you will not work will the URL it will work correctly. The problem is that hacker can try making $_GET['hello'] an array. If URL string looks like ?hello[]=something PHP will return error message

Warning: htmlspecialchars() expects parameter 1 to be string, array given in FILE on line 3

OK, who would enable in HTML errors in the production site (the other thing is error log...). But integers also are a problem - PHP is dynamically typed language, so it would accept easily code like '2' + 2. While yes, you can use (int) I have some old code which doesn't do that. If string comes from $_GET or $_POST it could be also array. Something like array('2') + 2 would cause fatal error of PHP.

Fatal error: Unsupported operand types in FILE on line 3

Now it's something that isn't acceptable because it would stop the script. I don't need arrays in those variables, but they annoy me. Is there any simple snippet which would remove arrays from those special variables. If I really would want an array, I could make copy of it before running the snippet.

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

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

发布评论

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

评论(3

吹泡泡o 2024-12-24 05:19:06

您可以使用 is_array() 来检查这样的事情。

if (is_array($_GET['hello']) // exit/error

或者只是再次显示表单

if (isset($_GET['hello']) && !is_array($_GET['hello']) {

至于有人输入诸如 array('2') + 2 之类的内容,您无需太担心。它已经是一个字符串,因此它不会作为 php 脚本运行。但是,根据您的范围,以最适合您的格式对信息进行编码始终是一个好主意。

在您的示例中,有人可以将 HTML 代码注入到您的 get 变量中,导致网站看起来不同。像 strip_tagshtmlentities 这样的简单函数不会有什么坏处。

You could use is_array() to check for such a thing.

if (is_array($_GET['hello']) // exit/error

or just display the form again

if (isset($_GET['hello']) && !is_array($_GET['hello']) {

As for someone inputting something like array('2') + 2 you don't need to worry much. It's already a string, therefor it won't be run as php script. However, depending on your scope is always a good idea to encode the information in a format best for you.

In you example, someone could inject HTML code into your get variable, causing the site to look different. Simple functions like strip_tags or htmlentities wouldn't hurt.

不必了 2024-12-24 05:19:06

在字符串上下文中使用它之前,我会检查它是否是字符串。

$name = (isset($_GET['name']) && is_string($_GET['name'])) ? $_GET['name'] : 'Unknown!';

或者:

if(isset($_GET['name']) && is_string($_GET['name']) {
    //do stuff
}

如果你想删除所有数组:

foreach($_GET as $key => $val) {
    if(is_array($val)) {
        unset($_GET[$key]);
    }
}

哎呀,看起来 Sudhir 已经打败了我,但已经输入了......:)

I would check if it was a string before using it in a string context.

$name = (isset($_GET['name']) && is_string($_GET['name'])) ? $_GET['name'] : 'Unknown!';

Or:

if(isset($_GET['name']) && is_string($_GET['name']) {
    //do stuff
}

If you wanted to remove all arrays though:

foreach($_GET as $key => $val) {
    if(is_array($val)) {
        unset($_GET[$key]);
    }
}

Oops look like Sudhir already beat me to that part, but already had it typed... :)

梦一生花开无言 2024-12-24 05:19:06

那么你能做这样的事情吗:

foreach($_POST as $key => $val) {
    if(is_array($val)) {
        unset($_POST[$key]);
    }
}

希望它在某种意义上有帮助。

Can you then do something like this:

foreach($_POST as $key => $val) {
    if(is_array($val)) {
        unset($_POST[$key]);
    }
}

Hope it helps in some sense.

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