PHP 处理跨域问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 处理跨域问题
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);
}
}
0%