Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add GET variables to end of the current url in php?

I am trying to add a few different GET variables to the url.

I could easily do a header redirect to the current page url and then just add the $_GET['test'] in the url.

My problem is that I have some GET variables that are in the url already. What I want to do is:

  • Check if there are any GET variables in the url

    • If there is not, then redirect to the current url with the new GET['test'] variable at the end of the url.

    • If there is, but there is no GET['test'] variable in the url, then keep those other GET values in the url and add the GET['test'] variable to end of the full url string

    • If there is, AND there is a GET['test'] variable in the url, then keep those other GET values in the url and exchange the GET['test'] variable value with the new value.

How can I go about checking for all these conditions?

like image 474
zeckdude Avatar asked Jul 02 '10 01:07

zeckdude


People also ask

How do I add a variable to 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.

What PHP variable is used to access URL variables?

PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL.

How can I get current URL in PHP?

To get the current page URL, PHP provides a superglobal variable $_SERVER. The $_SERVER is a built-in variable of PHP, which is used to get the current page URL. It is a superglobal variable, means it is always available in all scope.


2 Answers

The simple way to it is:

$params           = array_merge( $_GET, array( 'test' => 'testvalue' ) ); $new_query_string = http_build_query( $params ); 

This doesn't guarantee that test will be at the end. If for some odd reason you need that, you can just do:

$params = $_GET; unset( $params['test'] ); $params['test']   = 'testvalue'; $new_query_string = http_build_query( $params ); 

Note, however, that PHP query string parameter parsing may have some interoperability problems with other applications. In particular, PHP doesn't accept multiple values for any parameter unless it has an array-like name.

Then you can just forward to

( empty( $_SERVER['HTTPS'] ) ? 'http://' : 'https://' ) . ( empty( $_SERVER['HTTP_HOST'] ) ? $defaultHost : $_SERVER['HTTP_HOST'] ) . $_SERVER['REQUEST_URI'] . '?' . $new_query_string 
like image 192
Artefacto Avatar answered Sep 23 '22 03:09

Artefacto


I created this simple PHP function based on Artefacto's Answer.

function addOrUpdateUrlParam($name, $value) {     $params = $_GET;     unset($params[$name]);     $params[$name] = $value;     return basename($_SERVER['PHP_SELF']).'?'.http_build_query($params); } 
  • It updates the value if you are changing a existing parameter.
  • Adds up if it is a new value
like image 38
M_R_K Avatar answered Sep 23 '22 03:09

M_R_K