tags:

views:

104

answers:

5

I have a url,

example.com/?/page1

and i want to find out what the GET part of the url is, for example:

?/page1

how do i do this? like, without having to split strings and stuff?

+6  A: 

The following variable will contain the entirety of the query string (that is, the portion of the url following the ? character):

$_SERVER['QUERY_STRING']

If you're curious about the rest of the contents of the $_SERVER array, they're listed in the PHP manual here.

Amber
thanks, thats what i wanted
Jcubed
A: 

$_GET is an associative array of key values that are passed from the user's request. You can see its contents by doing:

print_r($_GET);

http://us3.php.net/manual/en/reserved.variables.get.php

EDIT: For convenience you may want to pass key=value pairs separated by ampersands. E.g.,

example.com/?page=1

Kirk
"example.com/?/page1" is actually a completely valid request (see http://tools.ietf.org/html/rfc3986#section-3.4) - it will invoke the page pointed to by "example.com/" and pass it the query string of "/page1". The key=value pair format is not required, it's simply a common query string format.
Amber
+2  A: 

That is a strange GET URL because the normal format is:

domain.com/page.html?a=1&b=2

PHPinfo will help a whole lot:

<?php phpinfo(); ?>

Output of relevance:

<?php
// imagine URL is 'domain.com/page.html?a=1&b=2'
phpinfo();
echo $_GET['a']; // echoes '1'
echo $_GET['b']; // echoes '2'
echo $_SERVER['QUERY_STRING']; // echoes 'a=1&b=2'
echo $_SERVER['REQUEST_URI']; // echoes '/path/to/page.php?a=1&b=2'
?>
Al
+2  A: 

Sounds to me like you want to look up parse_url() and parse_str(). Of course, these will 'split' the string(s) behind the scenes.

You can then use http_build_query to rebuild the query, if required.


 $url = "example.com/?/page1";
 $res = parse_url($url, PHP_URL_QUERY);
 print "Query:".$res."\n";

Output is:

Query:/page1
eloj
A: 
for($i = 0, $e = count($_GET[]); $i < $e; $i++) {
    echo $_GET[$i];
}
Tom