neubert
neubert

Reputation: 16792

behavior when there are two HTTP GET parameters with the same name

Say your HTTP request is formatted like this:

GET /path/to/file.php?var[]=1&var[]=2

PHP will populate that into an array named $_GET: array('var' => array(1, 2))

My question is... is this a PHP thing or is this behavior governed by an RFC? How would a web accessible node.js or Python script deal with this?

Upvotes: 2

Views: 44

Answers (2)

Marc B
Marc B

Reputation: 360662

PHP by default will overwrite previous values, and you end up with the LAST key:value pair found in the query string. But in PHP you can use the [] array as fieldname hack:

  1. example.com?foo=bar&foo=baz&foo=qux
  2. example.com?foo[]=bar&foo[]=baz&foo[]=qux

Line #1 produces $_GET like this:

$_GET = array(
    'foo' => 'qux'
);

Line #2 produces

$_GET['array'] = array
   'foo' => array('bar', 'baz', 'qux');
}

Note that this behavior is PHP specific. Other languages do their own thing. As far as I remember, Perl by default will keep all values as an array.

Upvotes: 2

oliakaoil
oliakaoil

Reputation: 1689

This is a specific feature of PHP with no related RFCs or other specs. I can't find any specific statements to that effect, but reading this FAQ Q/A seems to imply the same:

http://docs.php.net/manual/en/faq.html.php#faq.html.arrays

Upvotes: 1

Related Questions