Skip to content

Instantly share code, notes, and snippets.

@avtaniket
Last active May 6, 2024 19:02
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save avtaniket/9c435c25bc30af923ac7 to your computer and use it in GitHub Desktop.
Save avtaniket/9c435c25bc30af923ac7 to your computer and use it in GitHub Desktop.
Handle CORS in PHP
<?php
/* Handle CORS */
// Specify domains from which requests are allowed
header('Access-Control-Allow-Origin: *');
// Specify which request methods are allowed
header('Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS');
// Additional headers which may be sent along with the CORS request
header('Access-Control-Allow-Headers: X-Requested-With,Authorization,Content-Type');
// Set the age to 1 day to improve speed/caching.
header('Access-Control-Max-Age: 86400');
// Exit early so the page isn't fully loaded for options requests
if (strtolower($_SERVER['REQUEST_METHOD']) == 'options') {
exit();
}
@LucasHayashi
Copy link

Thanks!

@latiza
Copy link

latiza commented Dec 4, 2023

Thanks a lot!

@marcoczen
Copy link

Thanks....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment