29

How do I accept passed GET or POST values case insensitively?

Like sample.php?OrderBy=asc will still be the same as sample.php?orderby=asc or sample.php?ORDERBY=asc

Is there a means of achieving the above efficiently?

3
  • Not sure of a good solution, but I ran into a problem like that where the hashed password has gotten lower-cased and was unable to use it; so be careful, and I would suggest looking to see why this happens in the first place; usually when converting from HTTP to HTTPS (or back), the URL gets lower-cased. Commented Nov 18, 2010 at 3:32
  • 2
    What is the reason to change case? Commented Nov 18, 2010 at 3:32
  • 3
    well im working on an API where the system previously serves other API clients that use uppercase and recently we made API some changes including the decision to honor lower-cased parameters. so in order to maintain backward compatibility with the previous API users, i am faced with this kind of problem. Commented Nov 18, 2010 at 3:40

1 Answer 1

60

You could use array_change_key_case() to create a copy of $_GET with either all uppercase or all lowercase keys.

$_GET_lower = array_change_key_case($_GET, CASE_LOWER);
$orderby = isset($_GET_lower['orderby']) ? $_GET_lower['orderby'] : 'asc';
echo $orderby;

(I say "create a copy" simply because I don't like polluting the original superglobals, but it's your choice to overwrite them if you wish.)

For that matter, it'd still be better if you just stick to case-sensitive matching since it might be easier on both search engine and human eyes, as well as easier on your code... EDIT: OK, based on your comment I can see why you'd want to do something like this.

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

3 Comments

What is the reason to change case?
When launching the default web browser with a Shell command from VBA, I've had the url mysteriously changed to all lower case if the browser is the Windows 10 Edge browser. But not on all Windows 10 computers, only some.
I have been working on an API that needed to recognize that ID, Id and id are the same... Thanks @BoltClock

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.