Access-Control-Allow-Origin Multiple Origin Domains?

前端 未结 30 2064
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 07:08

Is there a way to allow multiple cross-domains using the Access-Control-Allow-Origin header?

I\'m aware of the *, but it is too open. I rea

30条回答
  •  名媛妹妹
    2020-11-21 07:35

    Here is what i did for a PHP application which is being requested by AJAX

    $request_headers        = apache_request_headers();
    $http_origin            = $request_headers['Origin'];
    $allowed_http_origins   = array(
                                "http://myDumbDomain.example"   ,
                                "http://anotherDumbDomain.example"  ,
                                "http://localhost"  ,
                              );
    if (in_array($http_origin, $allowed_http_origins)){  
        @header("Access-Control-Allow-Origin: " . $http_origin);
    }
    

    If the requesting origin is allowed by my server, return the $http_origin itself as value of the Access-Control-Allow-Origin header instead of returning a * wildcard.

提交回复
热议问题