Access-Control-Allow-Origin Multiple Origin Domains

In browser’s console get message:
no 'Access-Control-Allow-Origin' header is present on the requested resource. Origin is therefore not allowed access.
PHP source code: put into start of php script

// different DOMAINS
$http_origin = $_SERVER['HTTP_ORIGIN'];
 
if ($http_origin == "http://www.domain1.com" || $http_origin == "http://www.domain2.com" || $http_origin == "http://www.domain3.info")
{  
    header("Access-Control-Allow-Origin: $http_origin");
}
 
// OR different SUBDOMAINS
 $http_origin = $_SERVER['HTTP_ORIGIN']; // e.g. http://bla.yourdomain.com/index.php
 
$pos = strpos($http_origin, ".yourdomain.com");
$dom = ".yourdomain.com";
$le = strlen($dom);
$pos = strpos($http_origin, $dom);
 
if($pos !== false){
	$my_subdomains = substr($http_origin, 0, $pos + $le); // e.g. substr equal http://bla.yourdomain.com
    header("Access-Control-Allow-Origin: $my_subdomains");
}
?>
<!DOCTYPE HTML>
<html>
  <head>
  <meta............... etc.
This entry was posted in PHP and tagged , . Bookmark the permalink.


Comments are closed.

1,278 views