Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php REQUEST_URI

Tags:

php

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?

like image 760
user1118904 Avatar asked Dec 28 '11 07:12

user1118904


People also ask

What is Request_uri PHP?

$_SERVER['REQUEST_URI'] contains the URI of the current page. So if the full path of a page is https://www.w3resource.com/html/html-tutorials.php, $_SERVER['REQUEST_URI'] would contain /html/html-tutorials. php.

What does Request_uri return?

request_uri() Returns the equivalent of Apache's $_SERVER['REQUEST_URI'] variable. Because $_SERVER['REQUEST_URI'] is only available on Apache, we generate an equivalent using other environment variables.

What is $_ server Document_root?

$_SERVER['DOCUMENT_ROOT'] returns. The document root directory under which the current script is executing, as defined in the server's configuration file.

What is $_ server Http_user_agent?

The variable we are interested in right now is $_SERVER['HTTP_USER_AGENT'] . Note: $_SERVER is a special reserved PHP variable that contains all web server information. It is known as a superglobal. See the related manual page on superglobals for more information.


4 Answers

You can either use regex, or keep on using str_replace.

Eg.

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

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

Output will be: http://www.testing.com/123/123

like image 57
nand Avatar answered Oct 03 '22 08:10

nand


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.

like image 38
Oldskool Avatar answered Oct 03 '22 07:10

Oldskool


perhaps

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

and

$other_var = isset($_GET['othervar'])?$_GET['othervar']:null;
like image 28
cyberseppo Avatar answered Oct 03 '22 07:10

cyberseppo


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.

like image 39
תומי גורדון Avatar answered Oct 03 '22 08:10

תומי גורדון