多语言展示
当前在线:418今日阅读:195今日分享:41

php解决跨域问题

php怎么解决解决跨域问题,小编今天教你一个好方法!
方法/步骤
1

例如:客户端的域名是client.runoob.com,而请求的域名是server.runoob.com。

2

如果直接使用ajax访问,会有以下错误:XMLHttpRequest cannot load http://server.runoob.com/server.php. No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'http://client.runoob.com' is therefore not allowed access.

3

1、允许单个域名访问指定某域名(http://client.runoob.com)跨域访问,则只需在http://server.runoob.com/server.php文件头部添加如下代码:header('Access-Control-Allow-Origin:http://client.runoob.com');

4

2、允许多个域名访问指定多个域名(http://client1.runoob.com、http://client2.runoob.com等)跨域访问,则只需在http://server.runoob.com/server.php文件头部添加如下代码:$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';    $allow_origin = array(      'http://client1.runoob.com',      'http://client2.runoob.com'  );    if(in_array($origin, $allow_origin)){      header('Access-Control-Allow-Origin:'.$origin);       }

5

3、允许所有域名访问允许所有域名访问则只需在http://server.runoob.com/server.php文件头部添加如下代码:header('Access-Control-Allow-Origin:*');

推荐信息