Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling $_GET in codeigniter

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

It appears the framework deliberately 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.

like image 255
Nir Gavish Avatar asked Jan 11 '10 16:01

Nir Gavish


3 Answers

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');
like image 183
Stephen Curran Avatar answered Nov 09 '22 14:11

Stephen Curran


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).

like image 27
Gordon Avatar answered Nov 09 '22 13:11

Gordon


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.

like image 2
David Xia Avatar answered Nov 09 '22 15:11

David Xia