Download the PHP package jarir-ahmed/universal-cors without Composer
On this page you can find all versions of the php package jarir-ahmed/universal-cors. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jarir-ahmed/universal-cors
More information about jarir-ahmed/universal-cors
Files in jarir-ahmed/universal-cors
Package universal-cors
Short Description A secure-by-default, framework-agnostic CORS handler for PHP (plain PHP, Yii2, Laravel, Slim).
License MIT
Informations about the package universal-cors
universal-cors
A secure-by-default, framework-agnostic CORS handler for PHP. One small class that
answers preflight requests and emits the right Access-Control-* headers — without the
classic footguns (* + credentials, blanket origin reflection, forced debug mode).
Why
CORS is usually "fixed" by pasting header("Access-Control-Allow-Origin: *") plus
Access-Control-Allow-Credentials: true into a front controller. That combination is
forbidden by the spec and lets any website call your API with the user's cookies.
This package makes the safe path the easy path.
Requirements
- PHP >= 7.4
Install
Usage (plain PHP / any framework)
Put this at the very top of your front controller or API entry point:
Configuration
| Key | Default | Notes |
|---|---|---|
allowOrigins |
[] |
Exact origins, or ['*'] (only without credentials). |
allowOriginPatterns |
[] |
Wildcard patterns, e.g. https://*.example.com. * matches one DNS label and never crosses a dot. |
allowMethods |
GET, POST, PUT, PATCH, DELETE, OPTIONS |
|
allowHeaders |
Content-Type, Authorization |
Use ['*'] to reflect the browser's requested headers. |
exposeHeaders |
[] |
Response headers JS may read. |
allowCredentials |
false |
When true, the matching origin is reflected (never *). |
maxAge |
0 |
Preflight cache seconds. |
Safety guarantees
allowOrigins: ['*']withallowCredentials: truethrows on construction.- With credentials, the request
Originis reflected for allowed origins — never a blanket*— andVary: Originis set for correct caching. - Unlisted origins receive no
Access-Control-Allow-Originheader, so the browser blocks the response.
Testing without sending headers
resolve() and handle() are pure and return a CorsResult you can assert on:
Framework notes
- Plain PHP / Slim / custom: call
$cors->send()early;exiton preflight. - Yii2: prefer the built-in
yii\filters\Corsbehavior; use this package when you want one consistent CORS policy across non-Yii entry points too. - Laravel: register
send()in a global middleware'shandle()before the app runs.
License
MIT