Php REQUEST_URI

I have the following php script to read the request at the url:

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

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

But now I want to pass another variable in the url string http://www.testing.com/?id=123&othervar=123

How do I change the code above to get both variables?

+7
source share
5 answers

You can use regex or keep using str_replace.

Eg.

$url = parse_url($_SERVER['REQUEST_URI']);

if ($url != '/') {
    parse_str($url['query']);
    echo $id;
    echo $othervar;
}

The output will be: http://www.testing.com/123/123

+14
source

I think parse_str is what you are looking for, something like this should do the trick for you

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

Then the array $varswill contain all the arguments passed.

+5
source

,

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

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

vars, url, $_GET vars, filter_input():

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

var / .

0

You can just use it $_GETespecially if you know the name othervar. If you want to be safe, use if (isset ($_GET ['varname']))to check for existence.

0
source

All Articles