Php solutions to cross-domain issues

By setting Access-Control-Allow-Origin to achieve cross-domain.

For example: the client's domain name is client.runoob.com, and the requested domain name is server.runoob.com.

If you use direct access ajax, you will have the following error:

1 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.

1, allows a single domain access

Specify a domain name (http://client.runoob.com) cross-domain access, you can simply add the following code in the file header http://server.runoob.com/server.php:

1 header('Access-Control-Allow-Origin:http://client.runoob.com');

2, allows multiple domain names

Specify more than one domain name (http: //client1.runoob.com,http: //client2.runoob.com etc.) cross-domain access, you can simply file header in http://server.runoob.com/server.php add the following code:

 1 $origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';  
 2   
 3 $allow_origin = array(  
 4     'http://client1.runoob.com',  
 5     'http://client2.runoob.com'  
 6 );  
 7   
 8 if(in_array($origin, $allow_origin)){  
 9     header('Access-Control-Allow-Origin:'.$origin);       
10 } 

3, allows all domain names

Allow all domain names simply add the following code http://server.runoob.com/server.php file header:

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

Guess you like

Origin www.cnblogs.com/wyl0514/p/11392020.html