tags:

views:

69

answers:

2

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!

+2  A: 

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.

Kendall Hopkins
great! that really did it! thanks!!!ü
VeeBee
+1  A: 

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.

phsource
Kendall Hopkins
That's true; this function should preserve it only in the case of `?test=1 nevertheless, I haven't tested it, so I'm not certain of the behaviour in this case.
phsource