views:

42

answers:

4

Let's say I am getting requests such as:

http://www.example.com/index.php?id=123&version=3&id=234&version=4

Is it possible to extract these in a simple way inside my php code? I realize I could get the entire querystring with javascript using window.location.href and handle it manually but I'm looking for something more elegant. The requests can contain any number of version/id pairs but I can assume that the query is well-formed and have no obligation to handle invalid strings.

+2  A: 

According to this comment from the PHP manual, PHP's query string parser will drop duplicate params... so I don't think that PHP is a good fit for what you want to do (except in that it has the same capacity as javascript to get the raw query string, with which you can do whatever you want)

Brian Driscoll
+1 This seems pretty stupid on the PHP side, I think any other platform supports multivalued http parameters without problems.
leonbloy
There's a code example for a workaround function in that very comment, thank you.
MatsT
+1  A: 

If you can change the field name to include [], then PHP will create an array containing all of the matching values:

http://www.example.com/index.php?id{}=123&version[]=3&id[]=234&version[]=4

If you don't have the ability to change the field names, then as you say, you'll have to parse the querystring yourself.

JacobM
A: 

Check this post about using a built-in PHP function that builds a querystring from an array.

orvado
A: 

Assuming you have some control over the request, suffix the name with [] and PHP will generate arrays instead of dropping all but one.

http://www.example.com/index.php?id[]=123&version[]=3&id[]=234&version[]=4

Since they are pairs you'll probably want to fix the order they appear in using indexes.

http://www.example.com/index.php?id[0]=123&version[0]=3&id[1]=234&version[1]=4
David Dorward