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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3162725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:53:44  来源:igfitidea点击:

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

phpurlvariablesget

提问by zeckdude

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. What I want to do is:

我的问题是我已经在 url 中有一些 GET 变量。我想做的是:

  • 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.

  • 检查url中是否有任何GET变量

    • 如果没有,则使用 url 末尾的新 GET['test'] 变量重定向到当前 url。

    • 如果有,但 url 中没有 GET['test'] 变量,则将其他 GET 值保留在 url 中,并将 GET['test'] 变量添加到完整 url 字符串的末尾

    • 如果有,并且 url 中有一个 GET['test'] 变量,那么在 url 中保留那些其他 GET 值并将 GET['test'] 变量值与新值交换。

How can I go about checking for all these conditions?

我该如何检查所有这些条件?

回答by Artefacto

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 testwill be at the end. If for some odd reason you need that, you can just do:

这并不能保证test会在最后。如果出于某种奇怪的原因您需要这样做,您可以这样做:

$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.

但是请注意,PHP 查询字符串参数解析可能与其他应用程序存在一些互操作性问题。特别是,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

回答by M_R_K

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
  • 如果您正在更改现有参数,它会更新该值。
  • 如果是新值则相加

回答by N A

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.phpexample 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))];

回答by user2284001

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.

少了很多代码。

回答by nathan

..

..

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

回答by luky

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.

我写这个是因为与其他解决方案不同,它不会从 htaccess 重写中公开隐藏的 GET 参数。虽然它不会更新现有的参数。

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

回答by Amaynut

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 或更新它的值(如果它已经存在)