2010-01-11 28 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); 

享受:-)

相關問題