Codeigniter Controller return to previous page

Codeigniter Controller return to previous page

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

Stefan Bogdanescu

Founder & Senior Architect

Returning to the Previous Page in CodeIgniter Controllers

When building a multi-step form or handling an action that modifies data, you often want the user to be redirected back to the page they came from immediately after the operation completes. A common mistake is attempting to use a simple return statement inside the controller method. While this exits the current function, it does not break out of the request-response cycle initiated by CodeIgniter's Router. The framework will continue executing any subsequent logic in the controller and eventually attempt to load a view or render an empty response, leaving the user on the wrong page or seeing an error.

To achieve a redirect that actually sends the browser back to the previous URL, you must use CodeIgniter's built-in redirect() method. This function issues an HTTP 302 Found status code and sets the appropriate Location header, instructing the client to navigate to the specified destination.

The most robust approach is to pass the full URI string directly to the redirect helper. You can construct this URL dynamically based on where the request originated. Assuming you are using CodeIgniter 3 (the era when $this->load->view was standard syntax), the implementation inside your controller method would look like this:

public function someMethod($IDCustomer) {
    $this->Some_model->persist($IDCustomer);   
    
    // Redirect to the previous page using a relative path or full URI
    redirect('page_that_called_this_method'); 
    
    // The code below will not be executed if the redirect is successful
    // unless you explicitly disable it (which defeats the purpose here)
}

If your application structure relies on session data to track navigation, you might prefer a relative path. For instance, if someMethod was invoked from customers/index, you can simply return redirect('customers'). However, relying solely on relative paths can be fragile if the user clicks a link from an unexpected location or if the URL structure changes.

For more precision, especially when dealing with complex routing or nested controllers, you can retrieve the referring page using the HTTP headers available in CI's request object. While CodeIgniter 3 does not have a direct referer property like modern PHP environments, it is common practice to check the HTTP_REFERER header manually if needed:

$referer = $this->input->server('HTTP_REFERER');
if ($referer) {
    redirect($referer);
} else {
    redirect('/home'); // Fallback for initial page loads
}

This fallback is critical. Not all browsers send the Referer header, and some privacy-focused tools strip it by default. If you rely exclusively on the header without a fallback, users loading your form from a direct URL or navigating via a bookmark will encounter a "404 Not Found" if no route matches the stripped referer string.

Another consideration involves the execution flow of the controller. Once redirect() is called, the framework halts further processing in that specific method. If you have code immediately following the redirect call within the same function block, ensure it is guarded or removed to prevent logic errors. For example, if you were previously using $this->load->view('some_view.php') after the logic, that line will now be unreachable unless you are handling a conditional branch where the view should still render.

public function someMethod($IDCustomer) {
    $this->Some_model->persist($IDCustomer);   
    
    // Redirect back to previous page
    redirect('page_that_called_this_method');
    
    // This view will NOT load if the redirect is successful.
    // $this->load->view('some_view.php'); 
}

If you are migrating from CodeIgniter 3 to version 4, note that the helper function name changes to redirect() (which remains similar) but the underlying request handling logic has shifted significantly to a more modular architecture. In CI4, you would typically use $this->response->redirect()->back(). This method is specifically designed to redirect back to the previous URI and handles the fallback logic internally, making it less error-prone than manually checking headers.

Regardless of the version, the core principle remains: do not rely on simple return statements for navigation control in web frameworks. Always explicitly instruct the server to send a 302 response when the user's next step should be a page change.

Our network