Enable CORS for multiple domains in PHP

Today, we’ll explain to you how to permit CORS requests for multiple origins in PHP.

To get the response from a simple cross-origin POST request, we need to include the header Access-Control-Allow-Origin. The specification of Access-Control-Allow-Origin allows for multiple origins, or the value null, or the wildcard *.

Check the following code to enable CORS for multiple domains.

In the above code, we have taken an array of multiple domains which we want to allow in Access-Control-Allow-Origin.

Use the $_SERVER['HTTP_ORIGIN'] variable to verify which domain the request comes from and then conditionally set the Access-Control-Allow-Origin.

I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

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