cors跨域,同时设置了可传输cookie header("Access-Control-Allow-Credentials:true");
,
那么浏览器只允许 header("Access-Control-Allow-Origin:".$origin);
设置一个domain,并且不能用”*”,
这时候,获取访问的 HTTP_ORIGIN 用于判断,并动态地返回 Access-Control-Allow-Origin 的值,
就可以使用多个域名跨域啦,下面是代码:
1 2 3 4 5 6 7 8 9 10
| public function __construct() { $origin = $_SERVER['HTTP_ORIGIN']; if(strpos(G::$CORS_HOST,$origin)!==false || G::$CORS_HOST=='*'){ header("Access-Control-Allow-Origin:".$origin); } header("Access-Control-Allow-Credentials:true"); header("Access-Control-Allow-Methods:DELETE,GET,POST,PUT,OPTIONS"); header("Access-Control-Allow-Headers:x-requested-with,content-type"); header("Access-Control-Max-Age:1728000"); }
|