-
-
Save goranseric/2bf9b00715510666c08fa548c65b8e4f to your computer and use it in GitHub Desktop.
Access-Control-Allow-Origin Snipet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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