Skip to content

Instantly share code, notes, and snippets.

@brunoleles
Last active May 23, 2017 17:54
Show Gist options
  • Save brunoleles/11196662 to your computer and use it in GitHub Desktop.
Save brunoleles/11196662 to your computer and use it in GitHub Desktop.
Access-Control-Allow-Origin Snipet
<?php
// tells the CDN that the content 'Vary' depending on 'Origin' request header
header('Vary: Origin', true);
// allowed origins
$allowed_http_origins = array(
'http://example.com',
'http://www.example.com',
'http://127.0.0.1',
'http://localhost',
);
// get Origin sendend by the client if any
$http_origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : FALSE;
// check if the Origin is a allowed one
if ($http_origin !== FALSE && in_array($http_origin, $allowed_http_origins))
{
// set the Access-Control-Allow-Origin to allow the requested origin
header('Access-Control-Allow-Origin: ' . $http_origin, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment