How to get current url with its get request in CodeIgniter?

How to get current url with its get request in CodeIgniter?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Retrieving the Full Request URL and Query Parameters in CodeIgniter

When you are building a routing system or logging activity in a CodeIgniter application, there is often a distinction between knowing where you are in your application logic versus knowing exactly what was requested by the client. Consider a specific scenario where your current URL looks like http://myhost/main?my_id=1,3. You might need to reconstruct this exact string for logging purposes, or you specifically need to extract the my_id parameter to pass it to another function.

In CodeIgniter, accessing these pieces of information requires understanding how the framework handles the request object versus how it manages routing data. If you simply want the raw URL string exactly as it appeared in the browser address bar—including the host, path, and query string—you should look at the server's global input state rather than trying to rebuild it from your route segments.

The most direct way to get the full URL including GET parameters is by accessing the $_SERVER['REQUEST_URI'] superglobal combined with the base URL. However, CodeIgniter provides a cleaner abstraction for this via its built-in utility functions. The current_url() helper function is designed specifically to return the current path as requested by the client.

echo current_url(); 
// Output: http://myhost/main?my_id=1,3

This function automatically reconstructs the URL using the configuration settings defined in your .env file or config/config.php. It respects your base URL settings and appends the path and query string dynamically. If you are running into issues where this returns a relative path instead of an absolute one, ensure that your Config\App::$base_url is configured correctly to include the scheme (http/https) if needed, or explicitly pass the full URI if you require it without relying on global config defaults.

If your primary goal is to isolate the query parameters, such as extracting my_id, you should interact directly with the Input class. CodeIgniter parses the incoming HTTP request into an object that allows safe retrieval of data. Using $this->request->getGet() or accessing the $input instance provides a structured way to handle these values.

$input = $this->request->getGet();

// Accessing the parameter directly
$my_id = $input['my_id']; 
// Result: "1,3"

// Alternatively using helper functions for cleaner syntax
echo get('my_id'); 
// Result: "1,3"

This approach is distinct from retrieving raw $_GET data because it automatically handles the encoding and decoding of parameters based on your application's configuration. It also gives you access to additional metadata about the request, such as whether the parameter was sent via POST or GET, which can be crucial for debugging logic errors.

A common pitfall developers encounter is confusing the URL segment with the actual query string data. If your route is defined as main/{my_id}, CodeIgniter will strip the query parameters when you use $this->uri->segment(2), leaving you only with "1,3" but losing the context of how that value was passed. To maintain fidelity to the original request (http://myhost/main?my_id=1,3), always rely on current_url() for the full string representation.

When deciding between reconstructing a URL manually versus using helpers, consider performance and readability. While manual string concatenation works, it is prone to errors regarding trailing slashes or missing query parameters. Using the framework's native helpers ensures consistency across different environments, whether you are accessing the site via HTTP, HTTPS, or different subdomains.

If you find yourself needing more complex URL generation logic in the future, such as building URLs that include additional segments not yet in the current request, you might explore the url helper function. For instance, site_url('main') generates a full path based on your base URL settings. However, for retrieving the current state exactly as requested, sticking to current_url() and $this->request->getGet() remains the most robust strategy.

Our network