Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get multiple parameters with same name from a URL in PHP

I have a PHP application that will on occasion have to handle URLs where more than one parameter in the URL will have the same name. Is there an easy way to retrieve all the values for a given key? PHP $_GET returns only the last value.

To make this concrete, my application is an OpenURL resolver, and may get URL parameters like this:

ctx_ver=Z39.88-2004 &rft_id=info:oclcnum/1903126 &rft_id=http://www.biodiversitylibrary.org/bibliography/4323 &rft_val_fmt=info:ofi/fmt:kev:mtx:book &rft.genre=book &rft.btitle=At last: a Christmas in the West Indies.  &rft.place=London, &rft.pub=Macmillan and co., &rft.aufirst=Charles &rft.aulast=Kingsley &rft.au=Kingsley, Charles, &rft.pages=1-352 &rft.tpages=352 &rft.date=1871 

(Yes, I know it's ugly, welcome to my world). Note that the key "rft_id" appears twice:

  1. rft_id=info:oclcnum/1903126
  2. rft_id=http://www.biodiversitylibrary.org/bibliography/4323

$_GET will return just http://www.biodiversitylibrary.org/bibliography/4323, the earlier value (info:oclcnum/1903126) having been overwritten.

I'd like to get access to both values. Is this possible in PHP? If not, any thoughts on how to handle this problem?

like image 895
rdmpage Avatar asked Dec 09 '08 16:12

rdmpage


People also ask

How do you pass multiple parameters in a URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".

How do I pass multiple query parameters in REST URL?

Query parameters are passed after the URL string by appending a question mark followed by the parameter name , then equal to (“=”) sign and then the parameter value. Multiple parameters are separated by “&” symbol.

How many query string parameters can be added into a URL?

Although officially there is no limit specified by RFC 2616, many security protocols and recommendations state that maxQueryStrings on a server should be set to a maximum character limit of 1024.


1 Answers

Something like:

$query  = explode('&', $_SERVER['QUERY_STRING']); $params = array();  foreach( $query as $param ) {   // prevent notice on explode() if $param has no '='   if (strpos($param, '=') === false) $param += '=';    list($name, $value) = explode('=', $param, 2);   $params[urldecode($name)][] = urldecode($value); } 

gives you:

array(   'ctx_ver'     => array('Z39.88-2004'),   'rft_id'      => array('info:oclcnum/1903126', 'http://www.biodiversitylibrary.org/bibliography/4323'),   'rft_val_fmt' => array('info:ofi/fmt:kev:mtx:book'),   'rft.genre'   => array('book'),   'rft.btitle'  => array('At last: a Christmas in the West Indies.'),   'rft.place'   => array('London'),   'rft.pub'     => array('Macmillan and co.'),   'rft.aufirst' => array('Charles'),   'rft.aulast'  => array('Kingsley'),   'rft.au'      => array('Kingsley, Charles'),   'rft.pages'   => array('1-352'),   'rft.tpages'  => array('352'),   'rft.date'    => array('1871') ) 

Since it's always possible that one URL parameter is repeated, it's better to always have arrays, instead of only for those parameters where you anticipate them.

like image 51
Tomalak Avatar answered Sep 21 '22 14:09

Tomalak