CodeIgniter PHP框架-需要获取查询字符串

CodeIgniter PHP框架-需要获取查询字符串,php,codeigniter,frameworks,Php,Codeigniter,Frameworks,我正在使用创建一个电子商务网站 如何获取查询字符串 我正在使用支付网关。网关响应如下所示: http://www.test.com/registration/success/?DATA=<IDP+MSGTYPE%3D"PayConfirm"+KEYID%3D"1-0"+ID%3D"KI2WSWAn5UG3vAQv80AdAbpplvnb"+TOKEN%3D"(unused)"+VTVERIFY%3D"(obsolete)"+IP%3D" 123.25.37.43"+IPCOUNTRY%3D

我正在使用创建一个电子商务网站

如何获取查询字符串

我正在使用支付网关。网关响应如下所示:

http://www.test.com/registration/success/?DATA=<IDP+MSGTYPE%3D"PayConfirm"+KEYID%3D"1-0"+ID%3D"KI2WSWAn5UG3vAQv80AdAbpplvnb"+TOKEN%3D"(unused)"+VTVERIFY%3D"(obsolete)"+IP%3D" 123.25.37.43"+IPCOUNTRY%3D"IN"+AMOUNT%3D"832200"+CURRENCY%3D"CHF"+PROVIDERID%3D"90"+PROVIDERNAME%3D"Saferpay+Test+Card"+ACCOUNTID%3D"99867-94913159"+ECI%3D"2"+CCCOUNTRY%3D"XX"%2F>&SIGNATURE=bc8e253e2a8c9ee0271fc45daca05eecc43139be6e7d486f0d6f68a356865457a3afad86102a4d49cf2f6a33a8fc6513812e9bff23371432feace0580f55046c
$this->input->get('some_variable', TRUE);
http://www.test.com/registration/success/?DATA=&SIGNATURE=bc8e253e2a8c9ee0271fc45daca05eecc43139be6e7d486f0d6f68a356865457a3afad86102a4d49cf2f6a33a8fc6513812e9bff23371432feace0580f55046c
为了处理响应,我需要获取查询字符串数据


对不起,我没有把问题解释清楚。我在付款后从付款网站获得响应时遇到“未找到页面”错误

我尝试过在
config.php
中使用
uri\u协议='PATH\u INFO'
enable\u query\u strings='TRUE'
启用。在谷歌搜索时,我发现如果我使用htaccessrewrite,这将不起作用


我已经尝试更改配置项,但没有效果。

您可以这样得到:

http://www.test.com/registration/success/?DATA=<IDP+MSGTYPE%3D"PayConfirm"+KEYID%3D"1-0"+ID%3D"KI2WSWAn5UG3vAQv80AdAbpplvnb"+TOKEN%3D"(unused)"+VTVERIFY%3D"(obsolete)"+IP%3D" 123.25.37.43"+IPCOUNTRY%3D"IN"+AMOUNT%3D"832200"+CURRENCY%3D"CHF"+PROVIDERID%3D"90"+PROVIDERNAME%3D"Saferpay+Test+Card"+ACCOUNTID%3D"99867-94913159"+ECI%3D"2"+CCCOUNTRY%3D"XX"%2F>&SIGNATURE=bc8e253e2a8c9ee0271fc45daca05eecc43139be6e7d486f0d6f68a356865457a3afad86102a4d49cf2f6a33a8fc6513812e9bff23371432feace0580f55046c
$this->input->get('some_variable', TRUE);

如果需要未解析的查询字符串:

$this->input->server('QUERY_STRING');

打开application/config/config.php并设置以下值:

$config['uri_protocol'] = "PATH_INFO";

$config['enable_query_strings'] = TRUE; 

现在查询字符串应该可以正常工作了。

您可以在.htaccess中制定一个规则,以防止在特定页面上触发MOD_重写。这应该允许您使用_-GET。

我已经使用CodeIgniter一年多了。在大多数情况下,我真的很喜欢它(我为论坛做了贡献,并尽我所能使用它),但我讨厌手册中那句话的傲慢:

销毁全局GET数组。自从 CodeIgniter不使用GET 字符串,没有理由允许 它

假设您永远不需要进入CodeIgniter应用程序是愚蠢的!在短短几天内,我就不得不处理来自PayPal和ClickBank的回发页面(我肯定还有一百万其他页面)。猜猜看,他们使用GET

有很多方法可以阻止这种挤压,但它们往往会把其他事情搞砸。你不想听到的是,你必须重新编码你的所有视图,因为你启用了查询字符串,现在你的链接被破坏了!仔细阅读该选项的手册

我喜欢的一个(但因为在config.php中设置REQUEST_URI破坏了我的站点而无法工作)是扩展输入类:

class MY_Input extends CI_Input
{
        function _sanitize_globals()
        {
            $this->allow_get_array = TRUE;
            parent::_sanitize_globals();
        }
}
但最好的无意义方法是在需要GET变量的URL处使用print\r($\u SERVER)进行测试。查看哪个URI协议选项显示GET变量并使用它

在我的情况下,我可以看到我需要什么 请求URI

这会将查询字符串放回该页面实例的$\u GET super global中(您不必使用$\u GET,它可以是任何变量)

编辑

由于发布了这篇文章,我发现在使用REQUEST_URI时,除非删除?之前的所有内容,否则将丢失第一个查询字符串数组键?。例如,像/controller/method?one=1&two=2这样的URL将使用array('method?one'=>1,'two'=>2)填充本例中的$\u GET数组。为了解决这个问题,我使用了以下代码:

parse_str(substr(strrchr($_SERVER['REQUEST_URI'], "?"), 1), $_GET);
我想我应该提供一个例子,下面是:

class Pgate extends Controller {
   function postback() {
      parse_str(substr(strrchr($_SERVER['REQUEST_URI'], "?"), 1), $_GET);
      $receipt = $this->input->xss_clean($_GET['receipt']);
   }
}

您可以创建一个pre_系统挂钩。在您创建的钩子类中,您可以获取所需的查询参数,并将它们添加到$\u POST以进行正常的CI处理。我这样做是为了一个jqueryajax助手

例如:

(将此文件命名为autocomplete.php或您在钩子中作为文件名的任何名称)


感谢所有其他海报。这正是我想要的:

    $qs = $_SERVER['QUERY_STRING'];
    $ru = $_SERVER['REQUEST_URI'];
    $pp = substr($ru, strlen($qs)+1);
    parse_str($pp, $_GET);

    echo "<pre>";
    print_r($_GET);
    echo "</pre>";
在.htaccess中,我必须更改:

RewriteRule ^(.*)$ /index.php?/$1 [L]
致:


如果使用mod_rewrite删除index.php文件,可以使用以下代码获取GET变量(通过$this->input->GET()。假设默认配置,将文件命名为MY_Input.php并将其放置在应用程序/库目录中

用法:$this->input->get()

事实上,这是处理CodeIgniter中缺少对$\u GET查询字符串支持的最佳方法。事实上,我自己提出了这个,但很快就意识到Bretticus做了同样的事情,你必须稍微修改你处理第一个变量的方式:

$config['uri_protocol'] = "AUTO";
$config['enable_query_strings'] = FALSE;
在我亲自动手之前,这只是时间问题,但是使用这个方法是一个更好的单行解决方案,可以解决所有其他问题,包括修改现有的URI库,并且只对适用的控制器进行隔离,并且不必对默认配置(config.php)进行任何更改

有了这些,您现在可以使用以下各项:

print_r($_GET); // Array ( [field] => value ) 
验证结果:

<?php 
//adapt this code for your own use
                //added example.com to satisfy parse_url
        $url="http://www.example.com".$_SERVER["REQUEST_URI"];
        $url=parse_url($url);
                //I'm expecting variables so if they aren't there send them to the homepage
        if (!array_key_exists('query',$url))
        {
             redirect('/'); exit;
        }
        $query=$url['query'];

        parse_str($query,$_GET); //add to $_GET global array

        var_dump($_GET);
?>

下面是我最近是怎么做的。希望能有帮助

/system/application/config/config.php 


调用:
http://www.mydomain.com/mycontroller/myfunction/?somestuff=x&morestuff=y

下面是一个完整的工作示例,说明如何在Codeignitor中允许查询字符串,就像在JROX平台上一样。只需将其添加到位于以下位置的config.php文件:

$yo = $this->input->get('some_querystring', TRUE);
$yo = $_GET['some_querystring'];
然后,您可以简单地使用$\u get或下面的类像普通的一样获取查询字符串

/*
|--------------------------------------------------------------------------
| Enable Full Query Strings (allow querstrings) USE ALL CODES BELOW
|--------------------------------------------------------------------------*/

/*
|----------------------------------------------------------------------
| URI PROTOCOL
|----------------------------------------------------------------------
|
| This item determines which server global should 
| be used to retrieve the URI string.  The default 
| setting of 'AUTO' works for most servers.
| If your links do not seem to work, try one of 
| the other delicious flavors:
|
| 'AUTO'              Default - auto detects
| 'PATH_INFO'         Uses the PATH_INFO
| 'QUERY_STRING'      Uses the QUERY_STRING
| 'REQUEST_URI'   Uses the REQUEST_URI
| 'ORIG_PATH_INFO'    Uses the ORIG_PATH_INFO
|
*/
if (empty($_SERVER['PATH_INFO'])) {
    $pathInfo = $_SERVER['REQUEST_URI'];
    $index = strpos($pathInfo, '?');
    if ($index !== false) {
        $pathInfo = substr($pathInfo, 0, $index);
    }
    $_SERVER['PATH_INFO'] = $pathInfo;
}

$config['uri_protocol'] = 'PATH_INFO'; // allow all characters 

$config['permitted_uri_chars'] = ''; // allow all characters 

$config['enable_query_strings'] = TRUE; // allow all characters 

parse_str(substr(strrchr($_SERVER['REQUEST_URI'], "?"), 1), $_GET);
以下是使所有工作正常的代码:

$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$config['allow_get_array']      = TRUE;
$config['enable_query_strings'] = FALSE;
享受:-)

设置配置文件

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    Options -Indexes
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteCond $1 !^(index\.php)
    RewriteRule ^(.*)$ index.php [L]


</IfModule>
和.htaccess文件(根文件夹)

服务器端:


如果他这样做了,那么URL将需要将index.php放回其中。$\u SERVER['REQUEST\u URI']或$\u SERVER['QUERY\u STRING']?为什么还没有人将此标记为最佳答案?
<?php 
//adapt this code for your own use
                //added example.com to satisfy parse_url
        $url="http://www.example.com".$_SERVER["REQUEST_URI"];
        $url=parse_url($url);
                //I'm expecting variables so if they aren't there send them to the homepage
        if (!array_key_exists('query',$url))
        {
             redirect('/'); exit;
        }
        $query=$url['query'];

        parse_str($query,$_GET); //add to $_GET global array

        var_dump($_GET);
?>
/system/application/config/config.php 
$yo = $this->input->get('some_querystring', TRUE);
$yo = $_GET['some_querystring'];
/*
|--------------------------------------------------------------------------
| Enable Full Query Strings (allow querstrings) USE ALL CODES BELOW
|--------------------------------------------------------------------------*/

/*
|----------------------------------------------------------------------
| URI PROTOCOL
|----------------------------------------------------------------------
|
| This item determines which server global should 
| be used to retrieve the URI string.  The default 
| setting of 'AUTO' works for most servers.
| If your links do not seem to work, try one of 
| the other delicious flavors:
|
| 'AUTO'              Default - auto detects
| 'PATH_INFO'         Uses the PATH_INFO
| 'QUERY_STRING'      Uses the QUERY_STRING
| 'REQUEST_URI'   Uses the REQUEST_URI
| 'ORIG_PATH_INFO'    Uses the ORIG_PATH_INFO
|
*/
if (empty($_SERVER['PATH_INFO'])) {
    $pathInfo = $_SERVER['REQUEST_URI'];
    $index = strpos($pathInfo, '?');
    if ($index !== false) {
        $pathInfo = substr($pathInfo, 0, $index);
    }
    $_SERVER['PATH_INFO'] = $pathInfo;
}

$config['uri_protocol'] = 'PATH_INFO'; // allow all characters 

$config['permitted_uri_chars'] = ''; // allow all characters 

$config['enable_query_strings'] = TRUE; // allow all characters 

parse_str(substr(strrchr($_SERVER['REQUEST_URI'], "?"), 1), $_GET);
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$config['allow_get_array']      = TRUE;
$config['enable_query_strings'] = FALSE;
<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    Options -Indexes
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteCond $1 !^(index\.php)
    RewriteRule ^(.*)$ index.php [L]


</IfModule>
http://example.com/controller/method/param1/param2/?par1=1&par2=2&par3=x
http://example.com/controller/test/hi/demo/?par1=1&par2=2&par3=X
public function test($param1,$param2)
{
    var_dump($param1); // hi
    var_dump($param2); // demo
    var_dump($this->input->get('par1')); // 1
    var_dump($this->input->get('par2')); // 2
    var_dump($this->input->get('par3')); // X
}