CORS not working php

CORS not working php

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

Stefan Bogdanescu

Founder & Senior Architect

Why Your CORS Headers Are Being Ignored

When building a cross-domain application, it is easy to assume that setting the Access-Control-Allow-Origin header is sufficient to make an AJAX request work. However, if you are seeing an error like XMLHttpRequest cannot load... Origin ... is not allowed, the issue often lies not in the header itself, but in how your server responds to preflight requests or what other headers might be interfering with the response.

In the scenario described, the client attempts to send form data from www.siteone.com to www.sitetwo.com. The PHP script on the target server sets Access-Control-Allow-Origin: * and echoes a simple string. Despite this, the browser blocks the request. This behavior points to two distinct possibilities: an Apache-level misconfiguration overriding your PHP headers, or a missing preflight response.

The Apache Interceptor

The most critical clue in the provided headers is the presence of WWW-Authenticate Basic realm="Site two Server - Restricted Area" and the Content-Type being text/html; charset=iso-8859-1.

If you are running Apache with mod_authn_core or similar authentication modules enabled for that directory, Apache might be intercepting the response before your PHP script can send its custom headers. If a basic authentication realm is set up but no valid credentials are provided in the request, Apache will respond with a 401 Unauthorized status and include the WWW-Authenticate header to prompt the browser for login.

Browsers treat a 401 response as a failure regardless of what headers your PHP script sent earlier. The custom CORS headers are effectively discarded because the HTTP transaction is terminated by the authentication challenge before the application layer logic can complete. This explains why Access-Control-Allow-Origin appears to be ignored; it wasn't ignored, it was simply never received by the browser in a valid response context.

To fix this, you must ensure that the directory hosting cors.php does not have an active authentication realm unless you explicitly handle it. You can check your .htaccess file or Apache virtual host configuration for directives like AuthType Basic or Require valid-user. If these exist and are not intended for this endpoint, remove them or grant anonymous access using Require all granted.

Handling Preflight Requests

Even if the authentication issue is resolved, there is a second common pitfall: the preflight request. Modern browsers enforce CORS by sending an OPTIONS request before the actual POST if specific headers are involved (such as non-standard content types or custom headers).

Your PHP code currently handles POST, GET, and OPTIONS in the Access-Control-Allow-Methods header, but the response body is hardcoded to echo "hai". While this often works for simple GET requests, it can be problematic if the browser expects a specific status code (like 204 No Content) or if the response content type interferes with parsing.

More importantly, ensure that your server does not return a 200 OK with HTML content when handling an OPTIONS request in environments where Apache might inject its own default error pages or redirect logic. The response for an OPTIONS request should ideally be minimal:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type'); // Often required for form data
header('Content-Type: text/plain; charset=utf-8');
header('Vary: Origin');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    exit(0);
}

By exiting immediately after setting headers for an OPTIONS request, you prevent any accidental HTML output or authentication challenges from interfering.

Security Best Practices

Finally, while * is convenient for development, using it in production is a security risk, especially if the target server is not on a strict allowlist. A more robust approach is to specify the exact origin allowed:

$origin = $_SERVER['HTTP_ORIGIN'];
if ($origin) {
    header('Access-Control-Allow-Origin: ' . $origin);
}

This ensures that only requests from www.siteone.com (and its secure variant if HTTPS is used) receive the CORS headers, preventing other malicious sites from abusing your API.

If you are working within a Laravel ecosystem and need to handle these headers dynamically based on environment or route rules, consider leveraging packages like laravel/cors-middleware to automate this logic rather than managing raw headers in every controller or view file. This keeps your application code clean and ensures consistent behavior across different deployment environments.

Our network