Using query strings in CodeIgniter

View Comments
CodeIgniter logo

CodeIgniter is a great framework for PHP development, and part of it’s greatness is in it’s URL scheme. However, the URL scheme take a lot of getting used to, especially when you want to accomplish things that, while trivial before, become more difficult using CodeIgniter. One of these things is the use of query strings. By default, CodeIgniter may ignore your query strings, causing your $_GET superglobal array to be empty. Luckily, there is a fix.

CodeIgniter’s URL scheme

URLs in CodeIgniter are set up to call a method in a controller class, then pass the rest of the URL pieces as parameters of the method. This allows URLs to be mapped to function calls. So, when a user visits, it could be mapped to the action() method of the controller class, and pass the parameters as 1, 3, and foo, in order. So, the resulting call would be controller::action(1, 3, 'foo').

Conceptually, it’s genius. You get clean URLs with clean controller methods. The downside comes when you have an arbitrary amount of arguments, some of which must be defined and some of which do not have to be defined. So, let’s say that our above method action() accepts the parameters “$type_id,” “$amount_of_children,” and “$parent_name.” Lets say that these are used to filter database elements. Now, let’s say that we want the ability to only filter by some of these attributes and not all of them. Using a regular query string, we could just omit the attributes that we don’t want to filter by and include the attributes that we do want to filter by. But using CodeIgniter’s clean URLs, we would need to have some sort of default value, which is not always possible, or pretty as a URL.

So, in order to overcome this, we want to use a query string and our trusty $_GET superglobal array.

How to force CodeIgniter to recognize query string variables

The fix is simple. So simple, in fact, that all you have to do is edit your config file found in your codeigniter-folder/system/application/config/config.php directory, where “codeigniter-folder” is the folder that you have CodeIgniter installed in.

The item that we want to edit is the “uri_protocol” in your config.php file. By default, it should be set to “AUTO.” This will function differently for different servers, leaving some servers unable to access the variables in the query string. In order to consistently preserve the query string variables, set this value to “PATH_INFO.” So, the line in your config.php file should look like this:

| 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
$config['uri_protocol']	= "PATH_INFO";


I can only guarantee that this will work on a server that has mod_rewrite enabled. This includes Apache and IIS (though IIS requires mod_rewrite to be manually added). If the above method is not working, make sure that your .htaccess file has these lines in it:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Did you like this? Share it:
  • Caleb Williams

    CodeIgniter is a fantastic framework for PHP development, and aspect of it’s success is in it’s URL Schema.  The URL Schema take a lot of getting used to, especially when you want to achieve thing that, while simple before, become more challenging using CodeIgniter. One of these thing is the use of query String. By standard, CodeIgniter may neglect your query String.

  • nikz

    than how to pass values in hreaf link?
    like m using bla/bla/bla.php?id=43
    where id coming dynamicly

blog comments powered by Disqus