Skip to content

Instantly share code, notes, and snippets.

@f0t0n
Created May 30, 2012 15:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f0t0n/2836888 to your computer and use it in GitHub Desktop.
Save f0t0n/2836888 to your computer and use it in GitHub Desktop.
CORS example (server side).
<?php
if(empty($_SERVER['HTTP_ORIGIN'])) {
die;
}
defined('LOG_DIR') OR define('LOG_DIR', dirname(__FILE__) . '/log/');
// domain => apiKey dictionary
$allowedDomains = array(
'qualify-test-site.local' => 's0m3d0ma1n1d',
);
$host = parse_url($_SERVER['HTTP_ORIGIN'], PHP_URL_HOST);
if(!empty($host)
&& isset($allowedDomains[$host])
&& isset($_POST['apiKey'])
&& $allowedDomains[$host] == $_POST['apiKey']) {
$headers = array(
'Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN'],
'Access-Control-Allow-Methods: POST, GET, OPTIONS',
'Access-Control-Max-Age: 1000',
'Access-Control-Allow-Headers: Content-Type',
);
foreach($headers as $header) {
header($header);
}
}
$response = var_export($_POST, true);
header('Content-Length: ' . strlen($response));
header('Content-Type: text/plain; charset=UTF-8');
echo $response;
// log:
$separator = "\n" . str_repeat('-', 80) . "\n\n";
file_put_contents(
LOG_DIR . 'post.txt',
$response . $separator,
FILE_APPEND
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment