Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple HTTP GET parameters with the same identifier

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.

like image 297
MatsT Avatar asked Oct 20 '10 16:10

MatsT


2 Answers

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)

like image 147
Brian Driscoll Avatar answered Oct 11 '22 04:10

Brian Driscoll


Not as rounded or reliable as methods mentioned above but I use this to remove the need to [] in urls without worrying about rewriting.

$aQuery = explode("&", $_SERVER['QUERY_STRING']);
$aQueryOutput = array();
foreach ($aQuery as $param) {
    if(!empty($param)){
        $aTemp = explode('=', $param, 2);
        if(isset($aTemp[1]) && $aTemp[1] !== ""){
            list($name, $value) = explode('=', $param, 2);
            $aQueryOutput[ strtolower(urldecode($name)) ][] = urldecode(preg_replace('/[^a-z 0-9\'+-]/i', "", $value));
        }
    }
}
like image 43
atoms Avatar answered Oct 11 '22 02:10

atoms