I am trying to add a few different GET variables to the url.
我想在网址中添加一些不同的GET变量。
I could easily do a header redirect to the current page url and then just add the $_GET['test'] in the 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
检查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']变量,则将其他GET值保留在url中,并将GET ['test']变量值与新值交换。
-
How can I go about checking for all these conditions?
我怎样才能检查所有这些条件?
5 个解决方案
#1
34
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.
但请注意,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
#2
7
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
- 如果是新值,则添加
#3
1
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))];
#4
1
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.
代码少得多。
#5
0
..
..
$newval = 'whatever';
if( !count($_GET) ) {
header('Location: ?test=' . $newval);
exit;
}
if(!isset($_GET['test'])) {
$_SERVER['REQUEST_URI'] .= '&test='.$newval;
}
$_GET['test'] = $newval;
#1
34
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.
但请注意,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
#2
7
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
- 如果是新值,则添加
#3
1
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))];
#4
1
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.
代码少得多。
#5
0
..
..
$newval = 'whatever';
if( !count($_GET) ) {
header('Location: ?test=' . $newval);
exit;
}
if(!isset($_GET['test'])) {
$_SERVER['REQUEST_URI'] .= '&test='.$newval;
}
$_GET['test'] = $newval;