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¶m2=2, what's the easiest way to extract "param1=1¶m2=2"?
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.
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.
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.
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?
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¶m2=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¶m2=2
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With