Skip to content

Instantly share code, notes, and snippets.

@laocoi
Created April 22, 2019 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laocoi/54143a5d1cdb72d159997042b165fce4 to your computer and use it in GitHub Desktop.
Save laocoi/54143a5d1cdb72d159997042b165fce4 to your computer and use it in GitHub Desktop.
The following snippet should give you a quick overview about the required HTTP headers to set for CORS to work.
$allowedOrigins = array(
'(http(s)://)?(www\.)?my\-domain\.com'
);
if (isset($_SERVER['HTTP_ORIGIN']) && $_SERVER['HTTP_ORIGIN'] != '') {
foreach ($allowedOrigins as $allowedOrigin) {
if (preg_match('#' . $allowedOrigin . '#', $_SERVER['HTTP_ORIGIN'])) {
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
break;
}
}
}
Source: https://www.kerstner.at/2015/02/enabling-cross-origin-resource-sharing-cors-for-php/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment