Welcome Guest, Not a member yet? Register   Sign In
About Access-Control-Allow-Origin
#1

(This post was last modified: 01-18-2021, 12:39 AM by iot.)

Hello,

Thank your for reading my post. I would like to ask a question. I used Codeigniter 4. I used below code in my controller:
PHP Code:
public function __construct()
 {
     header("Access-Control-Allow-Origin: myhostname.com");

However, it still be accessible from other sites via Ajax?
Could you please tell me how can I limit access to myhostname.com only?
Reply
#2

(This post was last modified: 01-18-2021, 03:18 AM by InsiteFX.)

Also you should specify the url as "https://www.my.com"

Limiting the possible Access-Control-Allow-Origin values to a set of allowed origins requires code on the server side to check the value of the Origin request header, compare that to a list of allowed origins, and then if the Origin value is in the list, to set the Access-Control-Allow-Origin value to the same value as the Origin value.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 01-18-2021, 04:38 PM by iot.)

(01-18-2021, 03:18 AM)InsiteFX Wrote: Also you should specify the url as "https://www.my.com"

Limiting the possible Access-Control-Allow-Origin values to a set of allowed origins requires code on the server side to check the value of the Origin request header, compare that to a list of allowed origins, and then if the Origin value is in the list, to set the Access-Control-Allow-Origin value to the same value as the Origin value.

Dear InsiteFX,

Thank you for your response. I think when we specify the header, the web server should do the check. if we have to write the php code in controller to check it, why we need that header ?
Reply
#4

You can read all about CORS here on MDN.

Cross-Origin Resource Sharing (CORS)
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

Also note that AJAX CORS-checks are made using OPTIONS Method so you need to route that accordingly in CI.
Reply
#6

Thank you all
Reply




Theme © iAndrew 2016 - Forum software by © MyBB