PHP 处理跨域问题

2019-11-04  本文已影响0人  QinHaolei
// 处理跨域问题
header('Content-Type: text/html;charset=utf-8');
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin');

$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
// 允许的域名
$allow_origin = array(
    'localhost:9527',
    '其他允许跨域的域名'
);

for ($i = 0; $i < count($allow_origin); $i++) {
    if (strpos($origin, $allow_origin[$i]) > 0) {
        $str = 'Access-Control-Allow-Origin:' . $origin; 
        header($str);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读