PHP:如何检查查询字符串或POST变量是否包含两次相同的变量 [英] PHP: How to check if query string or POST vars contain same var twice
问题描述
使用$ _GET,$ _POST等访问变量时,PHP只返回给出每个变量名称的最后一个值。我无法找到一种方法来判断是否有任何变量出现超过一次。
我只需要知道查询字符串或POST正文中的变量是否包含
示例
我的应用程序应该为此查询字符串返回一个错误:
verb = ListIdentifiers& metadataPrefix = oai_dc& metadataPrefix = oai_dc
请注意,metadataPrefix是两次定义的。
我的应用程序不应该为这个查询字符串返回一个错误:
verb = ListIdentifiers& metadataPrefix = oai_dc
code>
$ input = file_get_contents('php:// input');
(或 $ HTTP_RAW_POST_DATA
( docs ))
GET请求
$ input = $ _SERVER ['QUERY_STRING'];
处理
explode('&',$ input)
并维护一个数组 - $ foundKeys
- 键(来自 explode()
> =
字符之前)。如果您点击 $ foundKeys
中已定义的键,则抛出错误。
It may sound strange, but in my PHP application I need to check if the same variable name has been declared more than once in the query string or POST variables, and return an error value if this is the case. If my application doesn't return an error in this case, it fails a compliance check.
When accessing vars using $_GET, $_POST, etc, PHP only returns the last value given for each variable name. I can't find a way to tell if any variable appeared more than once.
I simply need to find out if the query string or the variables in the POST body contained the same variable name more than once, whatever the values.
Example
My application is supposed to return an error for this query string:
verb=ListIdentifiers&metadataPrefix=oai_dc&metadataPrefix=oai_dc
Note that "metadataPrefix" is defined twice.
My application should not return an error for this query string:
verb=ListIdentifiers&metadataPrefix=oai_dc
POST Requests
$input = file_get_contents('php://input');
(Or $HTTP_RAW_POST_DATA
(docs))
GET Requests
$input = $_SERVER['QUERY_STRING'];
Processing
explode('&', $input)
and maintain an array - $foundKeys
- of keys (the part of each item from explode()
before the =
character). If you hit a key already defined in $foundKeys
, throw the error.
这篇关于PHP:如何检查查询字符串或POST变量是否包含两次相同的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!