PHP跨域设置

允许单个域名访问

指定某域名(http://client.xxx.com)跨域访问

php文件头部添加如下代码

header('Access-Control-Allow-Origin:http://client.xxx.com'); // 指定允许其他域名访问 //header('Access-Control-Allow-Origin:*'); // 响应类型 //header('Access-Control-Allow-Methods:POST,OPTIONS'); // 响应头设置 //header('Access-Control-Allow-Headers:X-Request-With,Content-Type'); //header('Access-Control-Allow-Credentials:true');

允许多个域名访问

指定多个域名(http://client1.xxx.comhttp://client2.xxx.com等)跨域访问

$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : ''; $allow_origin = array( 'http://client1.xxx.com', 'http://client2.xxx.com' ); if (in_array($origin, $allow_origin)) { header('Access-Control-Allow-Origin:'.$origin); }

允许所有域名访问

header('Access-Control-Allow-Origin:*');

创作不易,若本文对你有帮助,欢迎打赏支持作者!

 分享给好友: