开发者

Enabling $_GET in codeigniter

I've been trying to figure out how to enable $_GET in CI.

It appears the framework deliberately 开发者_Python百科destroys the $_GET array, and that enabling it requires serious tinkering with the core classes. can anyone say why this is, and how to overcome it?

mind you, i'm looking to keep URI parsing and routing the way they are, just simply have the $_GET available as well.


Add the following library to your application libraries. It overrides the behaviour of the default Input library of clearing the $_GET array. It allows for a mixture of URI segments and query string.

application/libraries/MY_Input.php

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

Its also necessary to modify some configuration settings. The uri_protocol setting needs to be changed to PATH_INFO and the '?' character needs to be added to the list of allowed characters in the URI.

application/config/config.php

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

It is then possible to access values passed in through the query string.

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


From the CodeIgniter's manual about security:

GET, POST, and COOKIE Data

GET data is simply disallowed by CodeIgniter since the system utilizes URI segments rather than traditional URL query strings (unless you have the query string option enabled in your config file). The global GET array is unset by the Input class during system initialization.

Read through this forum entry for possible solutions (gets interesting from halfway down page 1).


I don't have enough reputation to comment, but Phil Sturgeon's answer above is the way to go if switching to Codeigniter Reactor is easy for you.

You can access the query string by using $_GET or $this->input->get() without having needing the MY_Input override or even altering the config.php file.


On server, without PATH_INFO (just like mine) try this:

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

You can put it just like this:

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
}

}


I had success using this single line in my controller. It basically reparses the request URL without relying on any special CodeIgniter settings:

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


Never used $_GET with CI, better to change script logic to use POST or $this->uri->segment() , then to active $_GET params for me


From post: CodeIgniter PHP Framework - Need to get query string

Here's a full working example of how to allow querystrings in Codeignitor, like on JROX platform. Simply add this to your config.php file located at:

/system/application/config/config.php 

And then you can simply get the querystrings like normal using $_GET or the class below

$yo = $this->input->get('some_querystring', TRUE);
$yo = $_GET['some_querystring'];

Here's the code to make it all work:

/*
|--------------------------------------------------------------------------
| 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);

Enjoy :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜