How to enable cross-origin resource sharing using php

I was working on parsing KML data file from different resources which had different urls and Cesiumjs was unable to read/parse, that is how i come to know enabling cross-origin resource sharing.
It is also called CORS. Here is more details .

Following these steps you can enable “CORS”.
1. First check if you have apache module installed, using php.

  

2. Use command line to check if you have “Access-Control-Allow-Origin: *”

Command is - curl -I http://yourwebsite.com

3. If you don’t see this line “Access-Control-Allow-Origin: *” in curl -I output you need to add and enable it in either in PHP file ot .htacces file.

HTTP/1.1 302 Found
Date: Sat, 19 Sep 2015 00:31:41 GMT
Server: Apache
Access-Control-Allow-Origin: *
Set-Cookie: PHPSESSID=k1ejs4ra2gsu2id387h472one2; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: https://phpmind.com/index.php
Content-Type: text/html; charset=UTF-8
Set-Cookie: BIGipServer~DART-Dev~DART=1241032896.20480.0000; path=/

4. Now its time to add code and enable your missing part!

In .htaccess

 

    Header set Access-Control-Allow-Origin "*"

In PHP file

header("Access-Control-Allow-Origin: *"); // for all

header('Access-Control-Allow-Origin: http://www.rnai.technology'); // For particular URL
header('Access-Control-Allow-Origin: http://phpmind.com');
header('Access-Control-Allow-Origin: http://maps.google.com');

After that you need to test using

Command is - curl -I http://yourwebsite.com

You must see Access-Control-Allow-Origin: *” with headers.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *