1

The issue is that I want to keep the search variables that I used with a GET form when moving through the pages. It's like google does when you search for something and then you want to search for images, you just click on the images and don't need to replace the search. I did see that the link for their images or other kind of search already has the GET string in the url when the page is created. So is there a variable that contains everything from the url string and I can put it back in the href to the next page.

Example: my current url: localhost/index.php?name=John and I have a link to another page lets say edit.php It would be useful to put a code like echo 'edit.php?'.$GET_var.'; and this to echo to edit.php?name=John

EDIT: I will probably use $string = http_build_query($_GET); But another thing will be the get the part with index.php. So if I'm on this page(index.php) if the link to this page should be index.php and the other links to have the ?query arguments

user3864308
  • 25
  • 1
  • 9

4 Answers4

2

I'd recommend using the built-in parse_url.

$url ="http://localhost/index.php?name=John";
$result = parse_url($url);
$newUrl = 'http://localhost/index.php?' . $result['query'];

Or, maybe slightly cleaner, using the component argument of parse_url:

$url ="http://localhost/index.php?name=John";
$query = parse_url($url, PHP_URL_QUERY);
$newUrl = 'http://localhost/index.php?' . $query;

Read about parse_url.

netdigger
  • 3,659
  • 3
  • 26
  • 49
  • I will accept another answer, because it's includes the way you get the the url too. You did just declare yours as $url=.. This is useful too – user3864308 Aug 20 '14 at 06:40
1

you can try this

// suppose your url is index.php?name=john&city=jordan

$arr_temp = array();
foreach($_GET as $key=>$val)
{
   $arr_temp[] = $key."=".$val; // name=john or city=jordan into arr_temp array
}

$params = implode("&", $arr_temp); // implode with & name=john&city=jordan

echo 'edit.php?'.$params; // combine params to new link

UPDATE 2 :

$params = $_SERVER['QUERY_STRING'];
echo 'edit.php?'.$params;
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
  • 1
    I like this, but it could use a [`urlencode()`](http://ch2.php.net/manual/en/function.urlencode.php) or [`htmlspecialchars()`](http://ch2.php.net/manual/en/function.htmlspecialchars.php) during the assignment/output of the link. – ljacqu Aug 20 '14 at 06:26
  • 1
    You should check that `$key` isn't an array. you may run into problems on your second to last line when the url looks like this: `/index.php?name[]=hello` – Ryan Aug 20 '14 at 06:28
  • yeah probably. not sure if you would need to escape those values, never had to operate on the QUERY_STRING like that. – Ryan Aug 20 '14 at 06:32
  • doesn't need to escape to print in url it will be handled in the new page. where they will be access. – Satish Sharma Aug 20 '14 at 06:38
1

For moving $_GET variables from one page to other using php, you can use htt_build_query function as

$string = http_build_query($_GET);

Append that $string to your url i.e. YOUR_URL?$string

v2solutions.com
  • 1,439
  • 9
  • 8
  • +1 for this comment as it won't require explicitly appending the $_GET variables in links to other pages. – Basit Aug 20 '14 at 06:34
0

$_SERVER['QUERY_STRING'] has your GET variables. You might also want to look at phpinfo() to see what else you have available.

Ken
  • 77,016
  • 30
  • 84
  • 101