2010-01-11 31 views
10

我一直在想如何在CI中启用$ _GET。

看来框架故意破坏了$ _GET数组,并实现它需要的核心类严重的修修补补。任何人都可以说为什么这是,以及如何克服它?

你要知道,我期待继续URI解析和路由他们的方式,只让现有的$ _GET为好。

+1

你为什么要使用$ _GET变量时,你可以使用重新编写的网址为ACHI在CodeIgniter中有相同的用途吗? – GSto 2010-01-11 16:21:48

+2

好吧,我接受自己的唯一原因是支持传统的URL。我有一个非常希望移动到友好URL的客户端,可能存在使用框架重新执行他们的“意大利面代码”网站,但codeigniter不会允许他们的新网站支持几万(!)传入链接到各种文章,你明白为什么这是不可接受的:) – 2010-01-13 17:28:08

+0

新的解决方案是使用[CodeIgniter反应堆](https://bitbucket.org/ellislab/codeigniter-reactor),它支持开箱即用的GET。 – 2011-01-16 10:22:13

回答

14

将以下库添加到您的应用程序库。它覆盖了清除$ _GET数组的默认输入库的行为。它允许URI段和查询字符串的混合。

应用程序/库/ MY_Input.php

class MY_Input extends CI_Input 
{ 
    function _sanitize_globals() 
    { 
     $this->allow_get_array = TRUE; 
     parent::_sanitize_globals(); 
    } 
} 

它也需要修改一些配置设置。 uri_protocol设置需要更改为PATH_INFO和'?'字符需要添加到URI中允许的字符列表中。

应用/配置/ config.php中

$config['uri_protocol'] = "PATH_INFO"; 
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-?'; 

然后可以访问通过查询字符串传递值。

$this->input->get('x'); 
+0

使用PATH_INFO似乎无法在Windows XAMPP安装上使用。 – 2011-01-13 21:02:22

+1

就像CodeIgniter 2.x一样,$ this-> allow_get_array现在是$ this - > _ allow_get_array。 – jorisw 2013-01-09 13:43:06

+0

在CodeIgniter 2.x中,我不明白你为什么需要使用这个方法,因为在2.x中默认启用'$ _GET'参数。从CodeIgniter 2.x中的config.php: “默认情况下,CodeIgniter允许访问$ _GET数组。如果由于某种原因,您想禁用它,请将'allow_get_array'设置为FALSE。 – 2013-04-07 14:56:08

9

CodeIgniter's manual about security

GET,POST和cookie数据

GET数据由 笨因为该系统简单地不允许利用 URI段而不是传统的 URL的查询字符串(除非在 配置文件中启用了 查询字符串选项)。在初始化系统 期间,输入类未设置全局GET阵列 。

读通过这个forum entry for possible solutions会从中途下页1有趣)。

+0

谁低估了这一点,至少可以留下解释原因的评论。答案是正确的,并在给定的链接中提供替代方案。 – Gordon 2010-02-17 21:40:24

+0

这不是我:-S – 2010-02-17 23:09:46

1

在服务器上,而不PATH_INFO(就像我)试试这个:

parse_str(substr($_SERVER['REQUEST_URI'],strpos($_SERVER['REQUEST_URI'],'?')+1,strlen($_SERVER['REQUEST_URI'])-strpos($_SERVER['REQUEST_URI'],'?')),$_GET); 

你可以把它就像这样:

class Your_controller extends Controller { 

function Your_controller() 
{ 
    parent::Controller(); 

    date_default_timezone_set('Asia/Jakarta'); // set my timezone 

    parse_str(substr($_SERVER['REQUEST_URI'],strpos($_SERVER['REQUEST_URI'],'?')+1,strlen($_SERVER['REQUEST_URI'])-strpos($_SERVER['REQUEST_URI'],'?')),$_GET); 

} 

function test() 
{ 
    print_r($_GET); // here your $_GET vars 
} 

} 
2

我没有足够的声誉评论,但Phil Sturgeon's answer above是要走的路,如果切换到Codeigniter Reactor很容易。

您可以通过使用$ _GET或访问查询字符串$这个 - >输入 - 而不必需要的MY_Input覆盖,甚至改变config.php文件> get()方法。

1

我在控制器使用这种单线取得了成功。它基本上重新解析请求的URL,而不依赖于任何特殊的CodeIgniter设置:

parse_str(array_pop(explode('?',$_SERVER['REQUEST_URI'],2)),$_GET);

0

从未使用$ _GET与CI,不如改变使用POST脚本逻辑或$这个 - > URI->段()然后主动$ _GET params用于在我

0

从后:CodeIgniter PHP Framework - Need to get query string

这里是如何让查询字符串中Codeignitor,像JROX平台的完整工作示例。只需将它添加到位于你的config.php文件:

/system/application/config/config.php 

然后你就可以简单地得到像往常一样使用$ _GET或低于

$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); 

享受:-)

相关问题