Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Your plain PHP endpoint sets CORS headers but the browser still blocks it

ErrorAccess to XMLHttpRequest at 'https://api.example.com/save.php' from origin 'https://app.example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check

Short answer

The headers only help if they are sent on every response the browser looks at, starting with the OPTIONS preflight. In plain PHP the usual failures are code that runs the real work on OPTIONS, header() called after output, a wildcard origin used with cookies, and a header such as Authorization missing from the allowed list.

  • Applies to PHP 8.3, PHP 8.4, Apache 2.4, nginx 1.26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Send the preflight by hand and read the status and headers. The browser needs a 2xx status plus the Access-Control headers on this OPTIONS answer before it sends the real request.

    curl -i -X OPTIONS https://api.example.com/save.php \
      -H "Origin: https://app.example.com" \
      -H "Access-Control-Request-Method: POST" \
      -H "Access-Control-Request-Headers: content-type,authorization"
  2. 2Match what you see, then jump to its fix

    1. 1The preflight returns 401, 403, 405 or a PHP error, and has no Access-Control headersThe script runs its normal logic on the OPTIONS requestFix
    2. 2Warning: Cannot modify header information - headers already sent, and the CORS header is missingheader() is called after output has already startedFix
    3. 3The preflight works, then the real request is blocked; the page uses cookies or an Authorization headerA wildcard origin or missing Authorization in a credentialed callFix

What does the error look like?

  • Access to XMLHttpRequest at 'https://api.example.com/save.php' from origin 'https://app.example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check
  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/save.php:1)

Cause 1 of 3

The script runs its normal logic on the OPTIONS request

1Check

A preflight is an OPTIONS request with no cookies or token. If your script checks a session, validates a body or calls the database before it reaches the line that sets the headers, the answer is an error and the browser stops.

curl -s -o /dev/null -w "%{http_code}\n" -X OPTIONS https://api.example.com/save.php -H "Origin: https://app.example.com" -H "Access-Control-Request-Method: POST"

2Fix

Set the headers first, and end the OPTIONS request there with 204 before any other code runs.

<?php
$allowed = ['https://app.example.com', 'http://localhost:5173'];
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';

if (in_array($origin, $allowed, true)) {
    header("Access-Control-Allow-Origin: $origin");
    header('Vary: Origin');
    header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
    header('Access-Control-Allow-Headers: Content-Type, Authorization');
}

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

// normal code starts here

3Verify

The preflight now answers 204 with the headers present.

curl -si -X OPTIONS https://api.example.com/save.php -H "Origin: https://app.example.com" \
  -H "Access-Control-Request-Method: POST" | head -n 8

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 3

header() is called after output has already started

1Check

The PHP manual says header() must run before any output, and that includes HTML, a blank line or a space before the opening tag. An include file with trailing whitespace is a common source. The warning names the file and line where output began.

php -l save.php
head -c 20 save.php | xxd | head -n 2

2Fix

Remove the output before the headers: delete whitespace before <?php, drop the closing ?> from pure PHP files, and move the header() calls to the very top of the entry script. Output buffering is a stopgap, not a fix.

<?php
// line 1 of the file, nothing before it
header('Access-Control-Allow-Origin: https://app.example.com');

3Verify

The warning is gone and the header appears on a normal request.

curl -si https://api.example.com/save.php -H "Origin: https://app.example.com" | grep -i access-control

Confirmed by php.net

Did this fix it?

Cause 3 of 3

A wildcard origin or missing Authorization in a credentialed call

1Check

With cookies the response must name the exact origin and send Access-Control-Allow-Credentials: true; a wildcard is rejected, and wildcards in Allow-Headers or Allow-Methods are also refused in that mode. Authorization has to be listed by name in Access-Control-Allow-Headers.

curl -si -X OPTIONS https://api.example.com/save.php -H "Origin: https://app.example.com" -H "Access-Control-Request-Method: POST" -H "Access-Control-Request-Headers: authorization" | grep -i access-control

2Fix

Echo the allowed origin (from your own list), add the credentials header, and list the headers and methods explicitly.

header("Access-Control-Allow-Origin: $origin");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');

3Verify

Call the endpoint from the page with credentials: 'include'; the response is delivered to your script.

curl -si https://api.example.com/save.php -H "Origin: https://app.example.com" | grep -i "allow-credentials"

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Put the CORS block in one file included first by every endpoint, so no script forgets it.
  • Keep a PHP allowlist of origins; never echo the Origin header back without checking it.
  • Send Vary: Origin whenever the origin value changes per request.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

The browser blocks your API response: no Access-Control-Allow-Origin header

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.

4 causes, each with a fix

Back end

CodeIgniter works locally but every page is 404 on the live server

The requested URL was not found on this server.

A 404 on the live server comes from one of four places: the document root is not the public folder, Apache ignores .htaccess or nginx has no try_files rule, the route is not defined because auto routing is off, or the controller file name differs from its class in case.

5 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.