enabling cors in codeigniter ( restserver by @chriskacerguis )

enabling cors in codeigniter ( restserver by @chriskacerguis )

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

Stefan Bogdanescu

Founder & Senior Architect

Enabling CORS in CodeIgniter: Why Simple Headers Aren't Enough

When moving a local API to a production server, the browser's Same Origin Policy often throws an unexpected roadblock. You might have a working AngularJS frontend on localhost communicating seamlessly with your backend. Once that backend is hosted on a different domain, say example.com, the browser intercepts the request and blocks it immediately if the server doesn't explicitly allow it.

The error message is usually blunt: "Cross-Origin Request Blocked." Developers often react by adding headers directly in their controller constructors. A common attempt looks like this:

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET");

If you are using the RestServer library by @chriskacerguis, this approach frequently fails to resolve the issue, even though the headers seem present. The problem lies in how browsers handle "simple" versus "non-simple" requests and the specific order in which these headers must be sent.

The Missing Origin Header

The browser sends an Origin header with every cross-origin request. If you are performing a simple GET request without custom credentials or special content types, this header contains the URL of your client application (e.g., http://localhost:4200).

For the browser to accept the response, the server must echo back this specific origin in the Access-Control-Allow-Origin header. Setting this header to * is a valid strategy only if you are certain that no user data or sensitive information is being transmitted. However, even with *, the server must respond to the preflight check correctly before sending the actual data.

Preflight Requests and Method Mismatch

The critical failure point often involves the browser performing an "OPTIONS" request before the actual GET request. This is known as a preflight request. Before the browser fires your $http.get call, it sends an OPTIONS request to ask: "Are you okay with me sending a POST/GET from this origin?"

Your controller headers only allow GET:

header("Access-Control-Allow-Methods: GET");

If the client application (like AngularJS) configures its HTTP service to handle credentials or includes custom headers, the browser assumes the request is complex and sends an OPTIONS request first. If your server does not respond to the OPTIONS method with 200 OK and lists all allowed methods, the browser aborts the actual GET request immediately, resulting in the "Cross-Origin Request Blocked" error you are seeing.

To fix this in CodeIgniter, you need to handle the OPTIONS method specifically. In your controller's __construct, ensure that headers are set for all relevant methods, including OPTIONS:

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization"); // Often required for modern apps
header("Access-Control-Max-Age: 86400"); // Cache the preflight response

Implementing in RestServer

If you are using @chriskacerguis's RestServer, you should not rely solely on the constructor headers to handle dynamic routes. The library often intercepts requests before your custom headers run or assumes standard behavior that overrides them.

You must explicitly enable CORS support within the RestServer configuration or within the specific controller action handling the route. For example, inside a method in your Spots controller:

public function get_spots($id) {
    $this->load->model('spots_model');
    
    // Ensure this runs before any output is sent
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
    
    if ($this->input->method() === 'GET') {
        $data = $this->spots_model->get_spot($id);
        echo json_encode($data);
    } else {
        echo json_encode(['error' => 'Method not allowed']);
    }
}

The AngularJS Side

Finally, verify your client configuration. If you are using withCredentials: true in your AngularJS $http call, the server must also send Access-Control-Allow-Credentials: true and cannot use * for the origin header; it must specify the exact client origin. This is a strict security requirement enforced by modern browsers.

If the issue persists after adding these headers, check that you are not outputting any whitespace or BOM characters before setting the headers in your CodeIgniter file. The HTTP status line must be followed immediately by the headers. Any preceding echo statements will cause the server to terminate header transmission, rendering your CORS configuration ineffective.

For robust solutions in larger applications, consider leveraging dedicated security libraries or middleware that inject these headers consistently across all controllers, ensuring you don't miss a single route definition.

Our network