Sometimes, we want to add Cross-Origin Request Headers (CORS) with PHP headers.
In this article, we’ll look at how to add Cross-Origin Request Headers (CORS) with PHP headers.
How to add Cross-Origin Request Headers (CORS) with PHP headers?
To add Cross-Origin Request Headers (CORS) with PHP headers, we can call header
to add a few headers.
For instance, we write
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
to add the response headers with header
to enable CORS.
We specify that we enable it for all origins and when the request is done with GET or POST.
Conclusion
To add Cross-Origin Request Headers (CORS) with PHP headers, we can call header
to add a few headers.