[PHP] Access-Control-Allow-Origin Multiple Origin Domains

HYEONG HWAN, MUN/ 9월 30, 2016/ 미분류/ 0 comments

https://blog.lael.be/post/5076

Code snippet 용도의 글이다.

 

이 코드를 사용하여 허용된 곳에서의 xhr 요청을 제어할 수 있다.

기본적으로 브라우저 보안정책 중 하나인 CORS(Cross-Origin Resource Sharing) 설정 상황에서 사용한다.
요청 응답할 부분에서 허용된 Origin 을 설정해주어야 한다.

 

Access-Control-Allow-Origin 값이 멀티도메인을 허용하지 않기 때문에, 이렇게 프로그램적으로 작성해 주어야 한다.

 

$http_origin = $_SERVER['HTTP_ORIGIN'];

$allowed_origin = array('http://demosite.co.kr', 'http://www.demosite.co.kr', 'https://demosite.co.kr', 'https://www.demosite.co.kr');

if (in_array($http_origin, $allowed_origin))
{
    header("Access-Control-Allow-Origin: {$http_origin}");
}

Leave a Comment

작성하신 댓글은 관리자의 수동 승인 후 게시됩니다.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
*
*