Handling Parameters and Query Strings in URLs in CodeIgniter

URLs play a crucial role in web applications, as they allow users to navigate between different pages and perform various actions. In CodeIgniter, a popular PHP framework, handling parameters and query strings in URLs is a fundamental aspect of building robust and user-friendly web applications.

Parameters in URLs

Parameters in URLs are used to pass data from one page to another. In CodeIgniter, parameters can be defined in the URL by using the segment method of the input library. The segment method allows you to access different segments of the URL, where each segment represents a part of the URL separated by a forward slash.

To retrieve parameters from the URL, you can use the following syntax:


Here, n represents the segment number you want to retrieve. For example, if you have the following URL: example.com/users/profile/1, you can retrieve the parameter 1 by using:

$id = $this->input->segment(3);

In this example, 3 indicates that we want to retrieve the third segment of the URL.

Query Strings in URLs

Query strings are another way to pass data through URLs. A query string consists of a key-value pair, where the key and value are separated by an equal sign (=) and multiple parameters are separated by an ampersand (&). Query strings are typically used for filtering, sorting, or searching data.

To handle query strings in CodeIgniter, you can use the $_GET superglobal array, just like in regular PHP. CodeIgniter provides a helper function called get which simplifies retrieving query string parameters.

To retrieve query string parameters in CodeIgniter, you can use the following syntax:


Here, 'key' represents the name of the parameter you want to retrieve.

For example, if you have the following URL: example.com/search?keyword=codeigniter&page=1, you can retrieve the value of the keyword and page parameters as follows:

$keyword = $this->input->get('keyword');
$page = $this->input->get('page');

Now you can use these values to perform specific actions, such as filtering search results or paginating data.

URL Routing in CodeIgniter

CodeIgniter also provides URL routing functionality, which allows you to define custom URLs for your application. URL routing is particularly useful when you want to create clean and SEO-friendly URLs.

To define custom URLs in CodeIgniter, you can modify the routes.php file located in the config directory of your CodeIgniter application. In this file, you can specify the URL pattern and the corresponding controller and method to be invoked.

Here's an example of how to define a custom URL route in CodeIgniter:

$route['users/profile/(:num)'] = 'users/show_profile/$1';

In this example, users/profile/(:num) defines the URL pattern, where (:num) represents a parameter for the user ID. The users/show_profile/$1 specifies the controller (users) and method (show_profile) to be called, passing the parameter value ($1) to the method.

With URL routing, you can create user-friendly URLs that are easy to remember and understand. Additionally, it helps in managing the complexity of URLs with multiple parameters and query strings.


Handling parameters and query strings in URLs is a fundamental aspect of web development, and CodeIgniter provides a straightforward way to retrieve and work with them. By understanding how to handle parameters and query strings in URLs, you can enhance the functionality and user experience of your CodeIgniter applications. So, go ahead and leverage this feature in your next CodeIgniter project to create powerful and user-friendly web applications!

© NoobToMaster - A 10xcoder company