警告:filter_input():INPUT_REQUEST 尚未实施
Warning: filter_input(): INPUT_REQUEST is not yet implemented
我正在尝试修改这一行。
原来是
$cmd = $_REQUEST["cmd"];
然后,我通过在 Whosebug 上阅读此 post a link 更改为此。
$cmd = filter_input(INPUT_REQUEST, "cmd");
但是,我仍然遇到底部错误:
Warning: filter_input(): INPUT_REQUEST is not yet implemented
当我阅读其他文章 a link 时,它说 "INPUT_REQUEST is not a valid type."
这里的解决方案是什么?
根据文档(以及您的错误消息,以及您引用的 SitePoint 问答答案),它不是有效参数。 documentation says:
One of INPUT_GET
, INPUT_POST
, INPUT_COOKIE
, INPUT_SERVER
, or INPUT_ENV
.
这个函数没有INPUT_REQUEST
这样的东西。
What is solution here?
使用实际存在的参数之一。
TL;DR
如果您想使用 filter_input()
的过滤器选项,请选择 POST
、GET
或 COOKIE
作为您的请求类型。但这不是你唯一的选择。
注意: GET
通常用于发送检索数据请求的详细信息,而 POST
用于提交要存储的数据服务器。
您有几个选择:
确实,到目前为止(截至 PHP 7.2)仅实现了 INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV
。但是你仍然可以得到它的行为。
自己实现INPUT_REQUEST:(我不推荐这样做)你可以使用INPUT_GET
、INPUT_POST
和INPUT_COOKIE
通过 运行 实现您期望从当前不存在的 INPUT_REQUEST
获得的行为,如果没有得到任何 运行 下一个等等,如下所示。
`$test_filter = filter_input(INPUT_COOKIE, 'test') ?? filter_input(INPUT_POST, 'test') ?? filter_input(INPUT_GET, 'test');`
Use INPUT_GET or INPUT_POST: 如果您知道将使用 PUSH 或 GET 中的哪一个发送请求,则使用相应的选项。例如,将使用 INPUT_GET
检索 url 的查询字符串中发送的数据。
抑制超全局警告: 您可以通过转到 Windows 选择 netbeans 向您显示哪些警告:Tools -> Options -> Editor -> Hints
Mac: NetBeans -> Preferences -> Editor -> Hints
并取消选择 Superglobals
或您不想要的任何子集。
最后注意: filter_input()
有第三个参数是实际的过滤器部分;如果您无论如何都在使用 filter_input 那么您还不如实际使用过滤器。如果省略参数则:
If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. This will result in no filtering taking place by default.
如果您仍然需要以类似方式访问 $_REQUESTS
,您可以使用 filter_var()。
不过,您需要注意一些差异。
filter_var()
- Returns ""(空字符串)。
- (推荐)有一个
isset()
环绕。
filter_input()
- Returns 空。
示例
$cmd = '';
if ( isset( $_REQUEST["cmd"] ) ) {
$cmd = filter_var( $_REQUEST["cmd"], FILTER_SANITIZE_STRING );
}
请注意,根据这张票:https://bugs.php.net/bug.php?id=54672,INPUT_REQUEST
将永远不会实施。
INPUT_REQUEST
和 INPUT_SESSION
已在 PHP 8.0
中完全删除
如果您想要实现的是使用 filter_input 过滤 POST 和 GET 值,一个好的方法可能是这个:
$cmd = filter_input(INPUT_POST, "cmd");
if ($cmd){
$cmd = filter_input(INPUT_GET, "cmd");
}
知道 filter_input 的 return 值是:
,您甚至可以做一些更准确的事情
- false:如果过滤失败
- null:如果var_name变量没有设置
- 变量本身:如果一切正常
虽然答案已经被采纳,但这里是我在不知道或使用POST或使用GET时使用的另一种解决方案。
function getRequestValue($fieldName, $filter = FILTER_DEFAULT, $options = 0)
{
if (filter_input(INPUT_SERVER, "REQUEST_METHOD") == "POST") {
return filter_input(INPUT_POST, $fieldName, $filter, $options);
} else {
return filter_input(INPUT_GET, $fieldName, $filter, $options);
}
}
这样使用:
$cmd = getRequestValue("cmd");
其他选项类似于 https://www.php.net/manual/de/function.filter-input.php
我正在尝试修改这一行。
原来是
$cmd = $_REQUEST["cmd"];
然后,我通过在 Whosebug 上阅读此 post a link 更改为此。
$cmd = filter_input(INPUT_REQUEST, "cmd");
但是,我仍然遇到底部错误:
Warning: filter_input(): INPUT_REQUEST is not yet implemented
当我阅读其他文章 a link 时,它说 "INPUT_REQUEST is not a valid type."
这里的解决方案是什么?
根据文档(以及您的错误消息,以及您引用的 SitePoint 问答答案),它不是有效参数。 documentation says:
One of
INPUT_GET
,INPUT_POST
,INPUT_COOKIE
,INPUT_SERVER
, orINPUT_ENV
.
这个函数没有INPUT_REQUEST
这样的东西。
What is solution here?
使用实际存在的参数之一。
TL;DR
如果您想使用 filter_input()
的过滤器选项,请选择 POST
、GET
或 COOKIE
作为您的请求类型。但这不是你唯一的选择。
注意: GET
通常用于发送检索数据请求的详细信息,而 POST
用于提交要存储的数据服务器。
您有几个选择:
确实,到目前为止(截至 PHP 7.2)仅实现了 INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV
。但是你仍然可以得到它的行为。
自己实现INPUT_REQUEST:(我不推荐这样做)你可以使用INPUT_GET
、INPUT_POST
和INPUT_COOKIE
通过 运行 实现您期望从当前不存在的 INPUT_REQUEST
获得的行为,如果没有得到任何 运行 下一个等等,如下所示。
`$test_filter = filter_input(INPUT_COOKIE, 'test') ?? filter_input(INPUT_POST, 'test') ?? filter_input(INPUT_GET, 'test');`
Use INPUT_GET or INPUT_POST: 如果您知道将使用 PUSH 或 GET 中的哪一个发送请求,则使用相应的选项。例如,将使用 INPUT_GET
检索 url 的查询字符串中发送的数据。
抑制超全局警告: 您可以通过转到 Windows 选择 netbeans 向您显示哪些警告:Tools -> Options -> Editor -> Hints
Mac: NetBeans -> Preferences -> Editor -> Hints
并取消选择 Superglobals
或您不想要的任何子集。
最后注意: filter_input()
有第三个参数是实际的过滤器部分;如果您无论如何都在使用 filter_input 那么您还不如实际使用过滤器。如果省略参数则:
If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. This will result in no filtering taking place by default.
如果您仍然需要以类似方式访问 $_REQUESTS
,您可以使用 filter_var()。
不过,您需要注意一些差异。
filter_var()
- Returns ""(空字符串)。
- (推荐)有一个
isset()
环绕。
filter_input()
- Returns 空。
示例
$cmd = '';
if ( isset( $_REQUEST["cmd"] ) ) {
$cmd = filter_var( $_REQUEST["cmd"], FILTER_SANITIZE_STRING );
}
请注意,根据这张票:https://bugs.php.net/bug.php?id=54672,INPUT_REQUEST
将永远不会实施。
INPUT_REQUEST
和 INPUT_SESSION
已在 PHP 8.0
如果您想要实现的是使用 filter_input 过滤 POST 和 GET 值,一个好的方法可能是这个:
$cmd = filter_input(INPUT_POST, "cmd");
if ($cmd){
$cmd = filter_input(INPUT_GET, "cmd");
}
知道 filter_input 的 return 值是:
,您甚至可以做一些更准确的事情- false:如果过滤失败
- null:如果var_name变量没有设置
- 变量本身:如果一切正常
虽然答案已经被采纳,但这里是我在不知道或使用POST或使用GET时使用的另一种解决方案。
function getRequestValue($fieldName, $filter = FILTER_DEFAULT, $options = 0)
{
if (filter_input(INPUT_SERVER, "REQUEST_METHOD") == "POST") {
return filter_input(INPUT_POST, $fieldName, $filter, $options);
} else {
return filter_input(INPUT_GET, $fieldName, $filter, $options);
}
}
这样使用:
$cmd = getRequestValue("cmd");
其他选项类似于 https://www.php.net/manual/de/function.filter-input.php