什么是“更少的代码所需”的方式,以获得参数从一个URL查询字符串的格式如下?

www.mysite.com/category/subcategory ? myqueryhash

输出应该是:myqueryhash

我知道这种做法:

www.mysite.com/category/subcategory?q=myquery

<?php
   echo $_GET['q'];  //Output: myquery
?>

当前回答

下面是重建REFERRER查询字符串部分的函数。

如果调用页已经在它自己的URL中有一个查询字符串,你必须返回到那个页面,并想要返回一些而不是全部的$_GET变量(例如,一个页码)。

示例:引用者的查询字符串是?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'));

其他回答

函数parse_str()自动将所有查询参数读入数组。

例如,如果URL是http://www.example.com/page.php?x=100&y=200,则代码

$queries = array();
parse_str($_SERVER['QUERY_STRING'], $queries);

将参数值存储到$queries数组中($queries['x']=100, $queries['y']=200)。

查看parse_str的文档


EDIT

根据PHP文档,parse_str()只能与第二个参数(数组)一起使用。在这个URL上使用parse_str($_SERVER['QUERY_STRING'])将创建变量$x和$y,这使得代码容易受到诸如http://www.example.com/page.php?authenticated=1之类的攻击。

PHP的方法是使用parse_url函数,该函数解析URL并返回其组件。包括查询字符串。

例子:

$url = 'www.mysite.com/category/subcategory?myqueryhash';
echo parse_url($url, PHP_URL_QUERY); # output "myqueryhash"

完整的文档在这里

感谢@K。沙赫扎德。

当您希望重写查询字符串而不添加任何重写内容时,这将有所帮助。假设你重写/test/?X =y到index.php?Q =test&x=y,你只想要查询字符串。

function get_query_string(){

    $arr = explode("?", $_SERVER['REQUEST_URI']);
    if (count($arr) == 2){
        return "";
    }
    else{
        return "?" . end($arr) . "<br>";
    }
}
$query_string = get_query_string();

下面是重建REFERRER查询字符串部分的函数。

如果调用页已经在它自己的URL中有一个查询字符串,你必须返回到那个页面,并想要返回一些而不是全部的$_GET变量(例如,一个页码)。

示例:引用者的查询字符串是?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'));

$_SERVER['QUERY_STRING']包含您正在查找的数据。


文档

php.net: $_SERVER -手动