Enabling $_GET Variables when using CodeIgniter
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:
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.