开发者

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

I am tryi开发者_StackOverflowng 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?


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


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


function request_URI() {

    if(!isset($_SERVER['REQUEST_URI'])) {
        $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
        if($_SERVER['QUERY_STRING']) {
            $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
        }
    }
    return $_SERVER['REQUEST_URI'];
}

$_SERVER['REQUEST_URI'] = request_URI();

Courtesy: http://php.net/manual/en/reserved.variables.server.php example by LOL

This will give you the URL with respect to the root along with GET parameters.

In case you want it with respect to current directory, add the following.

$current_url = explode("/", $_SERVER['REQUEST_URI']);

$current_url = $current_url[end(array_keys($current_url))];


It may just be Joomla reasons, but I get the same value by using:

$currenturl = $_SERVER['REQUEST_URI'] ;

a lot less code.


..

$newval = 'whatever';
if( !count($_GET) ) {
 header('Location: ?test=' . $newval);
 exit;
}
if(!isset($_GET['test'])) {
 $_SERVER['REQUEST_URI'] .= '&test='.$newval;
}
$_GET['test'] = $newval;


I wrote this because unlike the other solution it doesn't expose the hidden GET parameters from htaccess rewrites. It doesn't update the existing param though.

 function addUrlParam($name, $value)
{
  $url = $_SERVER['REQUEST_URI'];
  $val =  $name . '=' . urlencode($value);
  if (strpos($url, '?') !== false) {
    $url .= '&' . $val;
  } else {
    $url .= '?' . $val;
  }
  return $url;
}


Here is the simplest way to do it

function buildUrl($name, $value)
 {
    $_GET[$name] = $value;
    return $_SERVER['SCRIPT_NAME'] . '?' . http_build_query($_GET);
 }

It will append the new query parameter to the URL or updates its value if it already exist

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜