$_GET parameters on CodeIgniter

Posted by Danny Herran on Dec 13, 2010 in Backend | 1 comment

Yeah, it is a much discussed topic in every CI forum. I needed to set up a payment gateway and it was mandatory to receive the gateway response via $_GET, so I decided to do an investigation on the matter and this is what I came up with.

Edit (Feb 13th 2011): This feature is now enabled by default on CI 2.0. If you want to learn about the changes in CI 2.0, click here.

By default CodeIgniter does not support $_GET parameters. They are automatically destroyed. There is an option in the config file to allow get parameters globally, however, this will prevent your “nice” urls to work as usual. Also, that config file option will enable $_GET parameters in every controller, which probably isn’t what we need.

What if we need only one controller to receive $_GET parameters? and how do we do it without hacking the CI code?

First of all, you have to modify your config file and change the uri_protocol from AUTO to PATH_INFO. This will allow CI to receive variables through the URL. It should look like this:

$config['uri_protocol'] =  'PATH_INFO';

Then, in every method you need to receive $_GET parameters, just add the following line:

parse_str($_SERVER['QUERY_STRING'], $_GET);

… which will re-populate the $_GET array. Now you are able to work with $_GET vars as you used to.

Your URLs will look something like this:

http://www.yourdomain.com/controller/method/?search_str=bananas&mode=advanced

Piece of cake right? Hope it helps!

Edit (Feb 13th 2011): This feature is now enabled by default on CI 2.0. If you want to learn about the changes in CI 2.0, click here.