Enabling $_GET Variables when using CodeIgniter

April 23rd, 2010 - Posted by Steve Marks to PHP, Web Development.

The PHP framework CodeIgniter, by default, does not allow the use of passing parameters via the URL query string, forcing developers to use URL segments to pass information between pages.

There are however some scenarios where you do need to access the $_GET array, for example when integrating with a payment gateway; The third party may pass the payment status, transaction reference and other crucial information back to you via the query string.

I found on some forums people suggesting that you enable query strings by changing the ‘enable_query_strings’ config variable. This however turned out to disable URL segments altogether and meant that controllers and methods had to be passed as a parameter in the URL also. Changing the way a whole pre-built system works for a one-off scenario shouldn’t be an option.

So how do we do it? Is it even possible? You’ll be glad to know the answer is yes, plus, it’s so easy. There are two changes we need to make. Firstly, in your CodeIgniter configuration ensure you ‘uri_protocol’ setting is as follows:

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

Note: If you do this and get a redirect loop, try skipping over this step and just perform the bit below.

Secondly, add the below code to your controller method where you are trying to access the query string:

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

The PHP parse_str function converts a string (in this case the query string) into a useable array. From now on, anywhere in your method you will be able to access $_GET variables as you would normally.

This entry was posted on Friday, April 23rd, 2010 at 6:54 pm by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.
Comments (0)
Comments are closed.