本文翻译自:Get URL query string parameters
What is the "less code needed" way to get parameters from a URL query string which is formatted like the following? 从格式如下的URL查询字符串中获取参数的“较少代码”方法是什么?
www.mysite.com/category/subcategory?myqueryhash www.mysite.com/category/subcategory?myqueryhash
Output should be: myqueryhash
输出应为: myqueryhash
I am aware of this approach: 我知道这种方法:
www.mysite.com/category/subcategory?q=myquery
<?php
echo $_GET['q']; //Output: myquery
?>
#1楼
参考:https://stackoom.com/question/ZXN9/获取URL查询字符串参数
#2楼
The function parse_str()
automatically reads all query parameters into an array. 函数parse_str()
自动将所有查询参数读入数组。
For example, if the URL is http://www.example.com/page.php?x=100&y=200
, the code 例如,如果URL为http://www.example.com/page.php?x=100&y=200
,则代码
$queries = array();
parse_str($_SERVER['QUERY_STRING'], $queries);
will store parameters into the $queries
array ( $queries['x']=100
, $queries['y']=200
). 会将参数存储到$queries
数组中( $queries['x']=100
, $queries['y']=200
)。
Look at documentation of parse_str 查看parse_str的文档
EDIT 编辑
According to the PHP documentation, parse_str()
should only be used with a second parameter. 根据PHP文档, parse_str()
仅应与第二个参数一起使用。 Using parse_str($_SERVER['QUERY_STRING'])
on this URL will create variables $x
and $y
, which makes the code vulnerable to attacks such as http://www.example.com/page.php?authenticated=1
. 在此URL上使用parse_str($_SERVER['QUERY_STRING'])
将创建变量$x
和$y
,这会使代码容易受到诸如http://www.example.com/page.php?authenticated=1
攻击的攻击。
#3楼
The PHP way to do it is using the function parse_url , which parses a URL and return its components. PHP的实现方法是使用parse_url函数,该函数解析URL并返回其组件。 Including the query string. 包括查询字符串。
Example: 例:
$url = 'www.mysite.com/category/subcategory?myqueryhash';
echo parse_url($url, PHP_URL_QUERY); # output "myqueryhash"
Full documentation here 完整的文档在这里
#4楼
Also if you are looking for current file name along with the query string, you will just need following 另外,如果您要查找当前文件名以及查询字符串,则只需执行以下操作
basename($_SERVER['REQUEST_URI'])
It would provide you info like following example 它将为您提供信息,例如以下示例
file.php?arg1=val&arg2=val file.php?arg1 = val&arg2 = val
And if you also want full path of file as well starting from root, eg /folder/folder2/file.php?arg1=val&arg2=val then just remove basename() function and just use fillowing 而且,如果您还希望从根目录开始也要有完整的文件路径,例如/folder/folder2/file.php?arg1=val&arg2=val,则只需删除basename()函数,然后使用fillowing
$_SERVER['REQUEST_URI']
#5楼
Here is my function to rebuild parts of the REFERRER's query string. 这是我的函数,用于重建REFERRER的查询字符串的一部分。
If the calling page already had a query string in its own URL , and you must go back to that page and want to send back some, not all, of that $_GET
vars (eg a page number). 如果调用页面已经在其自己的URL中具有查询字符串,则您必须返回该页面,并希望发送回一部分$_GET
变量(而不是全部)(例如,页码)。
Example: Referrer's query string was ?foo=1&bar=2&baz=3
calling refererQueryString( 'foo' , 'baz' )
returns foo=1&baz=3"
: 示例:引荐refererQueryString( 'foo' , 'baz' )
的查询字符串为?foo=1&bar=2&baz=3
调用refererQueryString( 'foo' , 'baz' )
返回foo=1&baz=3"
:
function refererQueryString(/* var args */) {
//Return empty string if no referer or no $_GET vars in referer available:
if (!isset($_SERVER['HTTP_REFERER']) ||
empty( $_SERVER['HTTP_REFERER']) ||
empty(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY ))) {
return '';
}
//Get URL query of referer (something like "threadID=7&page=8")
$refererQueryString = parse_url(urldecode($_SERVER['HTTP_REFERER']), PHP_URL_QUERY);
//Which values do you want to extract? (You passed their names as variables.)
$args = func_get_args();
//Get '[key=name]' strings out of referer's URL:
$pairs = explode('&',$refererQueryString);
//String you will return later:
$return = '';
//Analyze retrieved strings and look for the ones of interest:
foreach ($pairs as $pair) {
$keyVal = explode('=',$pair);
$key = &$keyVal[0];
$val = urlencode($keyVal[1]);
//If you passed the name as arg, attach current pair to return string:
if(in_array($key,$args)) {
$return .= '&'. $key . '=' .$val;
}
}
//Here are your returned 'key=value' pairs glued together with "&":
return ltrim($return,'&');
}
//If your referer was 'page.php?foo=1&bar=2&baz=3'
//and you want to header() back to 'page.php?foo=1&baz=3'
//(no 'bar', only foo and baz), then apply:
header('Location: page.php?'.refererQueryString('foo','baz'));
#6楼
For getting each node in the URI, you can use function explode()
to $_SERVER['REQUEST_URI']. 要获取URI中的每个节点,可以对$ _SERVER ['REQUEST_URI']使用函数explode()
。 If you want to get strings without knowing if it is passed or not. 如果要获取字符串而不知道是否传递了字符串。 you may use the function I defined myself to get query parameters from $_REQUEST (as it works both for POST and GET params). 您可以使用我自己定义的函数从$ _REQUEST获取查询参数(因为它同时适用于POST和GET参数)。
function getv($key, $default = '', $data_type = '')
{
$param = (isset($_REQUEST[$key]) ? $_REQUEST[$key] : $default);
if (!is_array($param) && $data_type == 'int') {
$param = intval($param);
}
return $param;
}
There might be some cases when we want to get query parameters converted into Integer type, so I added the third parameter to this function. 在某些情况下,我们希望将查询参数转换为Integer类型,因此我在此函数中添加了第三个参数。