{"id":1389,"date":"2015-09-19T00:35:21","date_gmt":"2015-09-19T00:35:21","guid":{"rendered":"http:\/\/www.phpmind.com\/blog\/?p=1389"},"modified":"2015-09-19T00:40:22","modified_gmt":"2015-09-19T00:40:22","slug":"how-to-enable-cross-origin-resource-sharing-using-php","status":"publish","type":"post","link":"https:\/\/www.phpmind.com\/blog\/2015\/09\/how-to-enable-cross-origin-resource-sharing-using-php\/","title":{"rendered":"How to enable cross-origin resource sharing using php"},"content":{"rendered":"<p>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.<br \/>\nIt is also called CORS. Here is more <a href=\"http:\/\/enable-cors.org\/\" target=\"_blank\">details <\/a>.<\/p>\n<p>Following these steps you can enable &#8220;CORS&#8221;.<br \/>\n1. First <a href=\"http:\/\/www.phpmind.com\/blog\/2015\/09\/how-to-check-which-apache-modules-are-installed-using-php\/\" target=\"_blank\">check<\/a> if you have apache module installed, using php. <\/p>\n<pre>\r\n  <?php\r\n    print_r(apache_get_modules());\r\n    ?>\r\n<\/pre>\n<p>2. Use command line to check if you  have &#8220;Access-Control-Allow-Origin: *&#8221; <\/p>\n<pre>Command is - curl -I http:\/\/yourwebsite.com<\/pre>\n<p>3. If you don&#8217;t see this line &#8220;Access-Control-Allow-Origin: *&#8221; in curl -I output you need to add and enable it in either in PHP file ot .htacces file.<\/p>\n<pre>\r\nHTTP\/1.1 302 Found\r\nDate: Sat, 19 Sep 2015 00:31:41 GMT\r\nServer: Apache\r\nAccess-Control-Allow-Origin: *\r\nSet-Cookie: PHPSESSID=k1ejs4ra2gsu2id387h472one2; path=\/\r\nExpires: Thu, 19 Nov 1981 08:52:00 GMT\r\nCache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0\r\nPragma: no-cache\r\nLocation: https:\/\/phpmind.com\/index.php\r\nContent-Type: text\/html; charset=UTF-8\r\nSet-Cookie: BIGipServer~DART-Dev~DART=1241032896.20480.0000; path=\/\r\n\r\n<\/pre>\n<p>4. Now its time to add code and enable your missing part!<\/p>\n<p>In .htaccess <\/p>\n<pre> \r\n<IfModule mod_headers.c>\r\n    Header set Access-Control-Allow-Origin \"*\"\r\n<\/IfModule>\r\n<\/pre>\n<p>In PHP file<\/p>\n<pre>\r\nheader(\"Access-Control-Allow-Origin: *\"); \/\/ for all\r\n\r\nheader('Access-Control-Allow-Origin: http:\/\/www.rnai.technology'); \/\/ For particular URL\r\nheader('Access-Control-Allow-Origin: http:\/\/phpmind.com');\r\nheader('Access-Control-Allow-Origin: http:\/\/maps.google.com');\r\n<\/pre>\n<p>After that you need to test using <\/p>\n<pre>Command is - curl -I http:\/\/yourwebsite.com<\/pre>\n<p>You must see Access-Control-Allow-Origin: *&#8221;  with headers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8220;CORS&#8221;. 1. First check if you have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[33,41,3],"tags":[],"class_list":["post-1389","post","type-post","status-publish","format-standard","hentry","category-htaccess","category-apache","category-php"],"_links":{"self":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/comments?post=1389"}],"version-history":[{"count":4,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1389\/revisions"}],"predecessor-version":[{"id":1393,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1389\/revisions\/1393"}],"wp:attachment":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/media?parent=1389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/categories?post=1389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/tags?post=1389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}