php 检查url是否包含参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10868818/
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
Check if url contains parameters
提问by Puyol
Possible Duplicate:
keeping url parameters during pagination
可能的重复:
在分页期间保留 url 参数
I want to add a parameter to the current url with php, but how do I know if the url already contains a parameter?
我想用php向当前url添加一个参数,但是我怎么知道这个url是否已经包含了一个参数?
Example:
例子:
foobar.com/foo/bar/index.php => foobar.com/foo/bar/index.php?myparameter=5 foobar.com/index.php?foo=7 => foobar.com/index.php?foo=7&myparameter=5
foobar.com/foo/bar/index.php => foobar.com/foo/bar/index.php?myparameter=5 foobar.com/index.php?foo=7 => foobar.com/index.php?foo =7&我的参数=5
The main problem is that I don't know if I need to add a "?".
主要问题是我不知道是否需要添加“?”。
My code (found it somewhere, but it doesn't work):
我的代码(在某处找到它,但它不起作用):
<?php if(/?/.test(self.location.href)){ //if url contains ?
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]&myparameter=5";
} else {
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?myparameter=5";
}?>
回答by Starx
The URL parameters are received from a global variable called $_GETwhich is in fact an array. So, to know if a URL contains a parameter, you can use isset()function.
URL 参数是从一个全局变量接收的,该变量$_GET实际上是一个数组。因此,要知道 URL 是否包含参数,您可以使用isset()函数。
if (isset($_GET['yourparametername'])) {
//The parameter you need is present
}
Afterwards, you can create separate array of such parameter you need to attach to a URL. Like:
之后,您可以创建需要附加到 URL 的此类参数的单独数组。喜欢:
if(isset($_GET['param1'])) {
\The parameter you need is present
$attachList['param1'] = $_GET['param1'];
}
if(isset($_GET['param2'])) {
$attachList['param2'] = $_GET['param2];
}
Now, to know whether or not, you need a ?symbol, just count this array
现在,要知道是否需要一个?符号,只需计算这个数组
if(count($attachList)) {
$link .= "?";
// and so on
}
Update:
更新:
To know if any parameter is set, just count the $_GET
要知道是否设置了任何参数,只需计算 $_GET
if(count($_GET)) {
//some parameters are set
}
回答by Lawrence Cherone
Really you should be using the parse_url()function:
你真的应该使用parse_url()函数:
<?php
$url = parse_url($_SERVER['REQUEST_URI']);
if(isset($url['query'])){
//Has query params
}else{
//Has no query params
}
?>
Also you should enclose your array based variables in curly brackets or break out of the string:
此外,您应该将基于数组的变量括在大括号中或打破字符串:
$url = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}?myparameter=5";
$url = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}?myparameter=5";
or
或者
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."?myparameter=5";
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."?myparameter=5";
enable error_reporting(E_ALL);and you will see the error. Notice: Use of undefined constant REQUEST_URI - assumed 'REQUEST_URI' ect
启用error_reporting(E_ALL);,您将看到错误。注意:使用未定义的常量 REQUEST_URI - 假定为“REQUEST_URI”等
回答by Moshe Shaham
you can search for the '?' char like this:
你可以搜索“?” 像这样的字符:
if (strpos($_SERVER[REQUEST_URI], '?')) { // returns false if '?' isn't there
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]&myparameter=5";
} else {
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?myparameter=5";
}

