tags:

views:

41

answers:

4

Hey,

I'm trying to redirect from one page to another while retaining the parameters.

e.g. if I have a page page.php?param1=1&param2=2, what's the easiest way to extract "param1=1&param2=2"?

+2  A: 

i would do

$querystring = '?'
foreach($_GET as $k=>$v) {
    $querystring .= $k.'='.$v.'&';
}
$url .= substr($querystring, 0, -1);

where $url already contains everything before the ?

you could also use $_SERVER['QUERY_STRING'] but as per the PHP manual:

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. *There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. *

Rob
That's technically true, but whatever web server he's using almost certainly provides `$_SERVER['QUERY_STRING']`; most PHP-based systems rely on it existing
Michael Mrozek
+3  A: 

Try $_SERVER['QUERY_STRING'].

sixfoottallrabbit
+3  A: 

$_SERVER['QUERY_STRING']

Source

Ben S
+4  A: 

Use $_SERVER['QUERY_STRING'] to access everything after the question mark.

So if you have the url:

http://www.sample.com/page.php?param1=1&param2=2

then this:

$url = "http://www.sample.com/page2.php?".$_SERVER['QUERY_STRING'];
echo $url;

will return:

http://www.sample.com/page2.php?param1=1&param2=2
Joseph