'php REQUEST_URI

I have the following php script to read the request in URL :

$id = '/' != ($_SERVER['REQUEST_URI']) ? 
    str_replace('/?id=' ,"", $_SERVER['REQUEST_URI']) : 0;

It was used when the URL is http://www.testing.com/?id=123

But now I wanna pass 1 more variable in url string http://www.testing.com/?id=123&othervar=123

how should I change the code above to retrieve both variable?

php


Solution 1:[1]

I think that parse_str is what you're looking for, something like this should do the trick for you:

parse_str($_SERVER['QUERY_STRING'], $vars);

Then the $vars array will hold all the passed arguments.

Solution 2:[2]

perhaps

$id = isset($_GET['id'])?$_GET['id']:null;

and

$other_var = isset($_GET['othervar'])?$_GET['othervar']:null;

Solution 3:[3]

You can simply use $_GET especially if you know the othervar's name. If you want to be on the safe side, use if (isset ($_GET ['varname'])) to test for existence.

Solution 4:[4]

Since vars passed through url are $_GET vars, you can use filter_input() function:

$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
$othervar = filter_input(INPUT_GET, 'othervar', FILTER_SANITIZE_FULL_SPECIAL_CHARS);

It would store the values of each var and sanitize/validate them too.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Oldskool
Solution 2 cyberseppo
Solution 3 norok2
Solution 4 Sergio