PHP:如何检查查询字符串或POST变量是否包含两次相同的变量 [英] PHP: How to check if query string or POST vars contain same var twice

查看:196
本文介绍了PHP:如何检查查询字符串或POST变量是否包含两次相同的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来很奇怪,但在我的PHP应用程序中,我需要检查是否在查询字符串或POST变量中多次声明了相同的变量名,并且如果是这种情况,则返回一个错误值。如果我的应用程序在这种情况下没有返回错误,它将无法执行合规性检查。



使用$ _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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆