Skip to content

Instantly share code, notes, and snippets.

@phpdave
Created August 26, 2016 20:54
Show Gist options
  • Save phpdave/c1d4f04e4c0752c379572372f61edf17 to your computer and use it in GitHub Desktop.
Save phpdave/c1d4f04e4c0752c379572372f61edf17 to your computer and use it in GitHub Desktop.
XMLHttpRequest cannot load https://differentSubDomain.example.com/myPHPScript.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www1.example.com' is therefore not allowed access.
<?php
$subject = $_SERVER['HTTP_ORIGIN'];
$subdomain1 = 'www';
$subdomain2 = 'www1';
$pattern = '/http(s)?:\/\/('.$subdomain1.'\.)?('.$subdomain2.'\.)?(example.com)$/';
$regexResult = preg_match($pattern, $subject);
if($regexResult)
{
header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
}
echo "Hi the browser has decided you're allowed to see this";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment