Your plain PHP endpoint sets CORS headers but the browser still blocks it
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 checkShort 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
The 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
Warning: Cannot modify header information - headers already sent, and the CORS header is missingheader() is called after output has already startedFix - 3
The 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
- 1
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 checkWarning: 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 here3Verify
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 8Confirmed by developer.mozilla.org
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 22Fix
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-controlConfirmed by php.net
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-control2Fix
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
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 fixBack end
Chrome shows net::ERR_INCOMPLETE_CHUNKED_ENCODING on a PHP page
The page loads partly, then the response stops
The browser got a chunked response that ended before its final zero-length chunk, so the connection closed mid-page.
2 causes, each with a fixBack 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 fixBack end
Express rejects a request body as too large
413 (Request Entity Too Large) returned by nginx in front of the app
A 413 means a body was bigger than a size limit.
3 causes, each with a fixBack end
Laravel file upload returns 413 behind nginx
413 Content Too Large
A 413 means a limit refused the request body before Laravel could use it.
3 causes, each with a fixBack end
Laravel CORS settings have no effect: missing config/cors.php, paths and cache
Response to preflight request doesn't pass access control check
Laravel answers CORS preflights itself through the HandleCors middleware, but only for URLs listed in the paths option of config/cors.php.
4 causes, each with a fix