简体   繁体   English

如何在 php 中将 GET 变量添加到当前 url 的末尾?

[英]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.我正在尝试向 url 添加一些不同的 GET 变量。

I could easily do a header redirect to the current page url and then just add the $_GET['test'] in the url.我可以轻松地将标题重定向到当前页面的 url,然后在 url 中添加 $_GET['test'] 。

My problem is that I have some GET variables that are in the url already.我的问题是我已经在 url 中有一些 GET 变量。 What I want to do is:我想做的是:

  • Check if there are any GET variables in the url检查url中是否有任何GET变量

    • If there is not, then redirect to the current url with the new GET['test'] variable at the end of the url.如果没有,则使用 url 末尾的新 GET['test'] 变量重定向到当前 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如果有,但 url 中没有 GET['test'] 变量,则将其他 GET 值保留在 url 中,并将 GET['test'] 变量添加到完整 url 字符串的末尾

    • 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.如果有,并且 url 中有一个 GET['test'] 变量,那么在 url 中保留那些其他 GET 值并将 GET['test'] 变量值与新值交换。

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.这并不能保证test会在最后。 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.但是请注意,PHP 查询字符串参数解析可能与其他应用程序存在一些互操作性问题。 In particular, PHP doesn't accept multiple values for any parameter unless it has an array-like name.特别是,PHP 不接受任何参数的多个值,除非它具有类似数组的名称。

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.我根据Artefacto的答案创建了这个简单的 PHP 函数。

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礼貌: http : //php.net/manual/en/reserved.variables.server.php示例由 LOL

This will give you the URL with respect to the root along with GET parameters.这将为您提供关于根的 URL 以及 GET 参数。

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:这可能只是 Joomla 的原因,但我使用以下方法获得了相同的值:

$currenturl = $_SERVER['REQUEST_URI'] ; $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.我写这个是因为与其他解决方案不同,它不会从 htaccess 重写中公开隐藏的 GET 参数。 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它将新的查询参数附加到 URL 或更新它的值(如果它已经存在)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM