setHeaderAccessControlAllowOrigin(). PHP의 cors 처리용

/**
 * setHeaderAccessControlAllowOrigin
 * CORS 처리용
 *
 * @param  boolean $anyone ture=아무나허용
 * @return void
 */
function setHeaderAccessControlAllowOrigin($anyone=false){
    if($anyone){
        header('Access-Control-Allow-Origin: *'); 
        return;
    }
    // CORS 설정
    $referer = isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:null;
    if(!$referer){
        // header('Access-Control-Allow-Origin: *'); 
    }else{
        $referer_info = parse_url($referer);
        $referer_scheme = $referer_info['scheme'];
        $referer_host = $referer_info['host'];
        $referer_port = isset($referer_info['port'])?$referer_info['port']:null; //int or null
        // var_dump($referer_port);
        if(strpos($referer_host,'domain.com')!==false || strpos($referer_host,'otherdomain.co.kr')!==false){
            $allow_domain = "{$referer_scheme}://{$referer_host}".(($referer_port!==null)?':'.$referer_port:'');
            header("Access-Control-Allow-Origin: {$allow_domain}"); 
        }else{
            // header('Access-Control-Allow-Origin: *'); 
        }
    }
}



댓글
  • No Nickname
    No Comment
  • 권한이 없습니다.
    {{m_row.m_nick}}
    -
목록형 📅 달력형