PHP header not working for Access-Control-Allow-Origin

PHP header not working for Access-Control-Allow-Origin

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

Stefan Bogdanescu

Founder & Senior Architect

PHP header not working for Access-Control-Allow-Origin

When configuring cross-origin file uploads between subdomains, such as admin.example.com and www.example.com, developers often encounter a frustrating scenario where the CORS headers appear correct in the source code but fail at runtime. A common symptom is an XMLHttpRequest error stating that the origin is not allowed by Access-Control-Allow-Origin. This issue frequently surfaces when using libraries like jQuery File Upload, especially if the server stack differs from standard PHP environments.

The root cause in your specific case lies in a mismatch between the expected response headers and what the web server is actually returning. Your PHP code explicitly sets Access-Control-Allow-Origin to http://admin.example.com. However, the HTTP response headers you captured for the failed OPTIONS request reveal a critical discrepancy:

Allow: OPTIONS, TRACE, GET, HEAD, POST
Content-Length: 0
Date: Tue, 27 Aug 2013 15:08:29 GMT
Public: OPTIONS, TRACE, GET, HEAD, POST
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET

Notice the absence of any Access-Control-Allow-Origin header in this response. Instead, the server identifies itself as Microsoft-IIS/7.5 and reports being powered by ASP.NET. This indicates that your PHP file (index.php) is likely not executing on the receiving server at all, or the request is being intercepted and handled by a different process entirely.

In a typical cross-domain upload flow initiated from JavaScript, the browser first sends an OPTIONS preflight request before the actual file data. If this request hits IIS instead of your PHP handler, the headers you set in index.php are never generated. The presence of X-Powered-By: ASP.NET confirms that the runtime environment is .NET, not PHP. Consequently, the browser receives no CORS headers and blocks the subsequent POST request for security reasons.

To resolve this, you must ensure that the OPTIONS request is routed to your PHP script on www.example.com. If IIS is handling the root of the domain or a specific path without forwarding requests to PHP, check your web server configuration (e.g., web.config). You may need to add rewrite rules to map incoming requests to your php-cgi.exe or ensure that .php files are processed by FastCGI rather than treated as static content.

If the routing is correct and IIS is indeed serving PHP, verify that the script has execute permissions and that no other middleware (like a load balancer or proxy) is stripping headers or terminating the connection before reaching your application logic.

Another potential pitfall involves the Access-Control-Allow-Credentials header. You have set this to true. When credentials are included in the request, the browser strictly forbids using the wildcard * for the Access-Control-Allow-Origin header. Your configuration uses the specific subdomain http://admin.example.com, which is compliant with the spec provided credentials are true. If you switch back to * while keeping Access-Control-Allow-Credentials: true, the browser will ignore the headers and reject the request immediately.

Ensure that your upload handler (UploadHandler.php) is also configured correctly within the PHP environment. The error might not be solely about the initial header response but also how the library handles subsequent requests. However, given the missing headers in the OPTIONS response, fixing the server routing to execute your PHP code is the primary priority.

If you are managing this stack and need a robust reference for handling such configurations in modern applications, consider looking at resources like https://laravelcompany.com for best practices on integrating file upload components with proper security headers and middleware configuration.

Our network