OMGKurtNilsen
OMGKurtNilsen

Reputation: 5098

PHP parse REQUEST_URI into array

I am parsing the REQUEST_URI into an array.

Right 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 concerns by doing it this way? Is this a good way of parsing it?

Edit: language

Upvotes: 12

Views: 21368

Answers (4)

ditto
ditto

Reputation: 6287

$the_array = explode('/', trim($_SERVER['REQUEST_URI'], '/'));

Upvotes: 3

mario
mario

Reputation: 145482

There is no security concern, but your solution is quite fiddly. It's already possible to accomplish that with parse_str (and parse_url for splitting up the path). Or in your case just:

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

Upvotes: 8

Shakti Singh
Shakti Singh

Reputation: 86406

You also can do with php in built functions. Which will be an efficient way.

$urlArr=parse_url($_SERVER['REQUEST_URI']);
parse_str($urlArr['query'], $output);

print_r($output);

Upvotes: 21

Adam Wright
Adam Wright

Reputation: 49386

Why not just use the $_GET dictionary?

Upvotes: 1

Related Questions