How to pass a data with redirect in codeigniter

How to pass a data with redirect in codeigniter

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

Stefan Bogdanescu

Founder & Senior Architect

Why redirect() Doesn't Carry Data the Way You Expect

In CodeIgniter, passing variables via a standard HTTP redirect using base_url() and URL segments often leads to confusion because of how the framework handles request initialization and parameter binding. The error "undefined variable" you are encountering stems from a misunderstanding of when those parameters become available to your function arguments versus how they are stored in the superglobal array.

When you execute $this->redirect(base_url()."home/index/".$in);, CodeIgniter does not simply pass the string value through as a hidden state. Instead, it generates a new HTTP 302 response and sends the browser to the specified URL. Upon receiving this redirect, the browser issues a brand new GET request to http://yoursite.com/home/index/1.

In this new request, CodeIgniter's router parses the URI segments before your controller logic even runs. The value 1 is extracted from the URI and placed into the global superglobal array $_GET['in']. However, CodeIgniter does not automatically inject this value into your function signature unless specific routing configurations are in place or if you explicitly access the input stream.

If you define your method as function index($in), CodeIgniter expects $in to be populated by its default parameter binding logic. In standard CodeIgniter 3.x, parameters are automatically bound from $_GET, $_POST, and $_REQUEST. However, if you are running into "undefined variable" errors, it is highly likely that one of two things is happening:

  1. Missing Routing Configuration: You might be using a routing configuration that expects specific patterns but isn't binding the parameters correctly for this specific segment.
  2. Strict Variable Checking: If your PHP settings have strict types or if you are accessing $in before the router has finished processing the request (which shouldn't happen in index, but can occur if logic is misplaced), the variable won't exist yet.

The most robust solution in CodeIgniter 3.x, without relying on URL rewriting quirks, is to explicitly access the input stream within your controller method. This ensures you are reading from the source that the router has populated. Instead of relying solely on the function argument $in, you can retrieve the value directly from the $_GET array inside the index() method:

function index()
{
    // Safely retrieve the parameter from the GET request
    $in = isset($_GET['in']) ? $_GET['in'] : 0;

    if ($in == 1) {
        // Your logic here
        echo "You are in mode 1";
    } else {
        echo "Default mode";
    }
}

By doing this, you decouple the method signature from the strict requirement of positional parameters, which can sometimes be fragile if the URL structure changes or if optional parameters are omitted.

Alternatively, if you prefer to keep the clean function signature function index($in), you must ensure that CodeIgniter's URI library is configured to bind these segments. In many modern setups, simply defining the parameter in the method signature works seamlessly because the framework automatically maps $in to $_GET['in']. If your current setup fails, check if you have a custom router file (application/routes.php or routes.php) that might be overriding default behavior or if you are using a version where automatic binding is disabled by default.

Another pitfall involves the use of query strings versus URI segments. Your current approach uses URI segments (/home/index/1). This is generally preferred for SEO and RESTful design, but it requires the segment to exist in the URL. If you strictly need to pass data without appending it to the URL path, you could use current_url() and append query parameters, though this is less elegant for internal navigation.

For more complex state passing where you want to avoid bloating your URLs with every single variable, consider using CodeIgniter's sessions or a dedicated flash message system if available in your specific version. However, for simple integer flags like $in, URI segments remain the standard practice. If you are looking for alternative frameworks that handle this out of the box with different philosophies, platforms like Laravel offer robust session management and request helpers that abstract these details further; you can explore similar patterns at https://laravelcompany.com if you decide to migrate or compare architectures.

Ultimately, the "undefined variable" error is a symptom of the router not having successfully mapped the URL segment to your method argument in the current execution context. Explicitly checking $_GET or verifying your routing configuration resolves the issue by ensuring the data exists before your logic attempts to use it.

Our network