Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - easiest way to get $_GET parameters string as written in the URL

Tags:

php

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"?

like image 785
bcoughlan Avatar asked Jun 29 '10 14:06

bcoughlan


People also ask

How can I get parameters from a URL string?

The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions. Note: Page URL and the parameters are separated by the ? character. parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it.

How do you find the variable in a URL?

To add a URL variable to each link, go to the Advanced tab of the link editor. In the URL Variables field, you will enter a variable and value pair like so: variable=value. For example, let's say we are creating links for each store and manager.

How can I get query string values in PHP?

To get the query string from a URL in php, you can use $_GET super global to get specific key value pairs or $_SERVER super global to get the entire string. A query string is a part of a URL that assigns values to specified parameters.

Are GET parameters always string?

Yes, URL query string params are of type string. It's up to you to convert them to and from the type you need. Are you trying to convert a number to string?


2 Answers

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
like image 180
Joseph Avatar answered Oct 12 '22 00:10

Joseph


In addition to Robs answer:

You can use http_build_query and $_GET.
This is build-in and can deal with arrays.
Also you can easily manipulate the GET params this way, befor you put them together again.

unset($_GET['unsetthis']);
$query = http_build_query($_GET);
like image 29
ivoba Avatar answered Oct 12 '22 01:10

ivoba