CodeIgniter PHP Framework - Need to get query string

CodeIgniter PHP Framework - Need to get query string

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

Stefan Bogdanescu

Founder & Senior Architect

Decoding Query Strings in CodeIgniter for Payment Gateways

Handling webhooks and payment confirmations requires precise handling of query strings, especially when dealing with gateways like Saferpay. The core issue here isn't just about reading the string; it's about how your server routes that request before CodeIgniter even sees it. If you are hitting a "Page not found" error when receiving a response from a payment provider, the configuration settings enable_query_strings and uri_protocol in config.php are likely being overridden by your .htaccess rewrite rules.

In CodeIgniter, the framework expects specific URL structures to map to controller methods. When a payment gateway sends a callback like http://www.test.com/registration/success/?DATA=..., it is sending a raw GET request with extensive parameters appended to the query string. Standard Apache or Nginx rewrites often strip these parameters or redirect them to a different path if strict routing rules are in place, causing the framework to interpret the URL as invalid.

The Routing Mismatch

The most common pitfall occurs when your .htaccess file contains a rule like RewriteRule ^(.*)$ index.php/$1 [L]. This rule attempts to rewrite every request to pass through index.php, but it often fails to preserve the full query string if the target controller doesn't explicitly expect it.

When you set enable_query_strings = TRUE in your application configuration, CodeIgniter switches from expecting a path-based URL (like /controller/method) to an array-based route (like /controller/method?var=value). This works for standard forms, but payment gateways often bypass traditional form submissions and send direct GET requests. If your server environment or .htaccess forces the request into a specific routing mode that conflicts with enable_query_strings, the framework will attempt to load a controller named success with parameters it doesn't know how to parse, resulting in a 404 error.

Extracting the Data Correctly

To resolve this, you must ensure the request reaches the controller method intact. Once inside your controller, specifically within the success action, you can access the raw query string data using the standard Input library. However, for complex payloads like Saferpay's response—which contains a long string with encoded parameters—you should treat the input as raw text before parsing.

Here is how to safely extract and decode the data:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Registration extends CI_Controller {

    public function success()
    {
        // Check if the request contains the expected Saferpay parameters
        if ($this->input->server('QUERY_STRING')) {
            
            // Get the raw query string without the leading '?'
            $query_string = substr($this->input->server('QUERY_STRING'), 1);
            
            // For debugging or logging purposes, you might want to decode it first
            // Note: Use simple_decode for basic decoding if the gateway uses URL encoding
            $decoded_data = urldecode($query_string);
            
            // Access specific fields directly from the input array
            $signature = $this->input->get('SIGNATURE');
            $idp_msgtype = $this->input->get('IDP_MSGTYPE');

            // Logic to validate signature and process payment
            if (validate_signature($signature)) {
                // Process order
            }
        } else {
            show_error('Invalid request: Query string missing.');
        }
    }
}

Verifying Configuration and Environment

If you have already modified application/config/config.php to set enable_query_strings = TRUE, verify that these changes are taking effect. Sometimes, multiple configurations or cached environments can mask these updates. Additionally, ensure that your .htaccess file does not contain aggressive rewrite rules that strip the query string before the request hits index.php.

You can test this by manually appending a parameter to your URL in a browser:
http://www.test.com/registration/success/?test=value

If you see a "Page not found" error here, the routing configuration is definitely misaligned with the gateway's delivery method. The solution often involves adjusting the .htaccess rules to pass the query string directly to the controller action without forcing a rewrite that discards parameters.

Alternative Approaches for Robustness

For high-stakes financial integrations like Saferpay, relying on standard GET parameters can sometimes be fragile due to browser caching or intermediate proxies stripping long query strings. A more robust pattern is to expect the gateway to POST the data or to configure your server to handle these specific endpoints as static routes that bypass CodeIgniter's default routing logic entirely.

If you are building a larger e-commerce platform, consider leveraging dedicated payment integration libraries that abstract away these low-level string manipulations. These libraries often handle signature validation and parameter parsing automatically, reducing the risk of human error in handling raw query strings.

For developers transitioning between frameworks, it is worth noting how Laravel handles similar scenarios. Laravel's routing engine is generally more flexible with query strings out of the box, but the underlying principle remains the same: ensure your server configuration allows the full request payload to reach the application logic without modification. If you find yourself wrestling with complex routing rules in CodeIgniter, switching to a framework with more modern routing defaults might be a consideration for future projects.

Our network