0

I have an api listener script which takes in get parameters. But I seem to be having issues when users tend to pass mixed case variable names on the parameters.

For example:

http://mylistenerurl.com?paramName1=Hello&paramname2=World

I need my listener to be flixible in such a way that the variable names will be interpreted case-insensitively or rather still all in lower case like after I process the query string on some function, they are all returned as lower-cased variables:

extract(someFunction($_GET));
process($paramname1, $paramname2);

Can anybody shed some light on this?

*much appreciated. thanks!

3 Answers 3

4

This should do the trick:

$array_of_lower_case_strings = array_map( "strtolower", array( "This Will Be ALL lowercase.", ... ) );

So in your case:

$get_with_lowercase_keys = array_combine(
    array_map( "strtolower", array_keys( $_GET ) ),
    array_values( $_GET )
);

One thing I'll mention is you should be VERY careful with extract as it could be exploited to allow unexpected variables to be injected into your PHP.

Sign up to request clarification or add additional context in comments.

Comments

1

Apply to your global variables ($_GET, $_POST) when necessary:

e.g. setLowerCaseVars($_GET); in your case

function setLowerCaseVars(&$global_var) {
    foreach ($global_var as $key => &$value) {
        if (!isset($global_var[strtolower($key)])) {
            $global_var[strtolower($key)] = $value;
        }
    }
}

Edit: Note that I prefer this to using array_combine because it will not overwrite cases where the lower-case variable is already set.

3 Comments

One of the values has to be "overwritten", you just chose to remove everyone after the first, instead of everyone before the last. PHP's $_GET defaults to "last one stands". For example ?test=1&test=2 $_GET["test"] === 2.
That's true; this function should preserve it only in the case of ?test=1&TEST=1; nevertheless, I haven't tested it, so I'm not certain of the behaviour in this case.
&$value is needless if you are going to be declaring directly to $global_var[strtolower($key)]. The modified array will still contain all of the non-lowercase keys in addition to the newly pushed elements with lowercase keys. In other words, this is going to bloat the array with redundant data.
0

PHP has had a native function (array_change_key_case()) for this task since version 4.2 to change the case of first level keys. To be perfectly explicit -- this function can be used to convert first level key to uppercase or lower case BUT this is not a recursive function so deeper keys will not be effected.

Code: (Demo)

parse_str('paramName1=Hello&paramname2=World&fOo[bAR][BanG]=boom', $_GET);

var_export($_GET);

echo "\n---\n";
$lower = array_change_key_case($_GET);

var_export($lower);

Output:

array (
  'paramName1' => 'Hello',
  'paramname2' => 'World',
  'fOo' => 
  array (
    'bAR' => 
    array (
      'BanG' => 'boom',
    ),
  ),
)
---
array (
  'paramname1' => 'Hello',        # N changed to n
  'paramname2' => 'World',
  'foo' =>                        # O changed to o
  array (
    'bAR' =>                      # AR not changed because not a first level key
    array (
      'BanG' => 'boom',           # B and G not changed because not a first level key
    ),
  ),
)

1 Comment

Additionally, before calling extract() on user supplied data, you need to filter the keys against a whitelist of safe keys -- I recommend array_intersect_key().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.