PHP parse REQUEST_URI to array

I am parsing REQUEST_URI into an array.

Now I have the following code:

private function load_url_vars() { if (preg_match('/^.+\?(.+)$/', $_SERVER["REQUEST_URI"], $matches)) { $varpairs = preg_split("/&/", $matches[1]); foreach ($varpairs as $varpair) { if (preg_match('/^([A-Za-z_]+)=(.*)$/', $varpair, $varmatch)) { $this->urlvars[$varmatch[1]] = urldecode($varmatch[2]); } } } } 

Are there any security issues doing this this way? Is this a good way to take it apart?

Change the language

+8
php regex
source share
4 answers

There are no security issues, but your solution is rather inconvenient. This can be done using parse_str (and parse_url to split the path). Or in your case, simply:

 list($path, $qs) = explode("?", $_SERVER["REQUEST_URI"], 2); parse_str($qs, $this->urlvars); 
+6
source share

You can also do with php built-in functions. This will be an effective way.

 $urlArr=parse_url($_SERVER['REQUEST_URI']); parse_str($urlArr['query'], $output); print_r($output); 
+12
source share

Why not just use the $_GET dictionary ?

+1
source share
 $the_array = explode('/', trim($_SERVER['REQUEST_URI'], '/')); 
+1
source share

All Articles