11

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.

Nir Gavish
  • 1,034
  • 3
  • 13
  • 28
  • 1
    why do you want to use $_GET variables, when you can use re-written URLs to achieve the same purpose in CodeIgniter? – GSto Jan 11 '10 at 16:21
  • 2
    well, the only reason i would accept myself would be to support legacy URLs. i have a client who would like very much to move to friendly-URLs, the possibility exists to re-do their "spaghetti code" website using a framework, but codeigniter will not allow their new site to support several tens of thousand (!) incoming links to various articles, you understand why this is unacceptable :) – Nir Gavish Jan 13 '10 at 17:28
  • The new solution is to use [CodeIgniter Reactor](https://bitbucket.org/ellislab/codeigniter-reactor), which supports GET properly out of the box . – Phil Sturgeon Jan 16 '11 at 10:22

7 Answers7

14

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');
Stephen Curran
  • 7,433
  • 2
  • 31
  • 22
  • Using PATH_INFO doesn't seem to work on Windows XAMPP installs. – Thomas Hunter II Jan 13 '11 at 21:02
  • 1
    As if CodeIgniter 2.x, $this->allow_get_array is now $this->_allow_get_array . – jorisw Jan 09 '13 at 13:43
  • In CodeIgniter 2.x I don't see why you'd need to use this method at all, because in 2.x `$_GET` parameters are enabled by default. From config.php in CodeIgniter 2.x: "By default CodeIgniter enables access to the $_GET array. If for some reason you would like to disable it, set 'allow_get_array' to FALSE." – Matt Browne Apr 07 '13 at 14:56
8

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

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Whoever downvoted this could at least leave a comment explaining why. The answer is correct and provides alternatives in the given link. – Gordon Feb 17 '10 at 21:40
2

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
}

}
Abdul Rahman
  • 2,097
  • 4
  • 28
  • 36
matriphe
  • 21
  • 1
2

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.

Community
  • 1
  • 1
David Xia
  • 5,075
  • 7
  • 35
  • 52
1

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

Andreas Gohr
  • 4,617
  • 5
  • 28
  • 45
0

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

itsme
  • 48,972
  • 96
  • 224
  • 345
0

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

Community
  • 1
  • 1
Rob Vanders
  • 505
  • 1
  • 5
  • 13