php 解决跨域配置

php如果解决跨域问题呢?这是一个问题!

下面请看配置代码

$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
$allow_origin = array(
    'http://xxxxxx.com',
    'http://yyyyy.com',
);
if(in_array($origin, $allow_origin)){
    header('Access-Control-Allow-Origin:'.$origin); //接受那些域名下的跨域 注意如果$origin=* 是不支持cookie跨域的
};
header('Access-Control-Allow-Credentials:true');  // 允许跨域发送cookie
header("Access-Control-Allow-Headers: authorization, Origin, X-Requested-With, Content-Type, Accept, Set-Cookie");
//以下代码是过滤 请求方法  与跨域五官
header('Access-Control-Allow-Methods: POST,GET');

if(strtoupper($_SERVER['REQUEST_METHOD'])== 'OPTIONS'){
    exit;
}

猜你喜欢

转载自blog.csdn.net/jackbon8/article/details/103665394