Skip to content

Instantly share code, notes, and snippets.

@stiucsib86
Created September 26, 2013 05:20
Show Gist options
  • Save stiucsib86/6710147 to your computer and use it in GitHub Desktop.
Save stiucsib86/6710147 to your computer and use it in GitHub Desktop.
Simple ping test for cross-domain request.
<?php
header("Content-Type: application/json");
// Allow Credentials
header('Access-Control-Allow-Credentials: true');
// Allow all origins
if (isset($_SERVER['HTTP_REFERER']) || isset($_SERVER['HTTP_ORIGIN'])) {
if (isset($_SERVER['HTTP_REFERER'])) {
$url_components = parse_url($_SERVER['HTTP_REFERER']);
} else if (isset($_SERVER['HTTP_ORIGIN'])) {
$url_components = parse_url($_SERVER['HTTP_ORIGIN']);
}
if (isset($url_components['port'])) {
header('Access-Control-Allow-Origin: ' . $url_components['scheme'] . '://' . $url_components['host'] . ':' . $url_components['port']);
} else {
header('Access-Control-Allow-Origin: ' . $url_components['scheme'] . '://' . $url_components['host']);
}
} else {
header('Access-Control-Allow-Origin: *');
}
// Specify which request methods are allowed
header('Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS');
// Additional headers which may be sent along with the CORS request
// The X-Requested-With header allows jQuery requests to go through
//header('Access-Control-Allow-Headers: X-Requested-With');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
//// Exit early so the page isn't fully loaded for options requests
//if (strtolower($_SERVER['REQUEST_METHOD']) == 'options') {
// exit();
//}
echo json_encode(array("Message" => "OK!"));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment