Skip to content

Instantly share code, notes, and snippets.

@wilcorrea
Created August 27, 2020 14:03
Show Gist options
  • Save wilcorrea/67f49b809b32d6e100c1cd05a8d95a4d to your computer and use it in GitHub Desktop.
Save wilcorrea/67f49b809b32d6e100c1cd05a8d95a4d to your computer and use it in GitHub Desktop.
<?php
# allow all methods
header('Access-Control-Allow-Methods: POST, GET, PUT, PATCH, DELETE, OPTIONS');
# allow some headers
header('Access-Control-Allow-Headers: Authorization, Bearer, Device, Origin, Accept, Content-Type');
# allow expose some headers
header('Access-Control-Expose-Headers: Authorization, Bearer, Device');
# get the request method
$method = $_SERVER['REQUEST_METHOD'] ?? null;
# check if method is OPTIONS...
if (strtoupper($method) === 'OPTIONS') {
# ...detects if there is an origin
$origin = $_SERVER['HTTP_ORIGIN'] ?? '*';
# allow origin
header("Access-Control-Allow-Origin: {$origin}");
# allow credentials (optional)
header('Access-Control-Allow-Credentials: true');
exit(1);
}
// your code here...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment