Files
Retribusi/api/cors-handler.php

31 lines
989 B
PHP
Raw Normal View History

<?php
/**
* CORS Handler untuk API Btekno
*
* File ini HARUS di-include di awal SETIAP endpoint PHP
* atau ditempatkan di file bootstrap/autoload yang dieksekusi sebelum semua endpoint
*
* Usage:
* require_once __DIR__ . '/cors-handler.php';
*/
// Set CORS headers
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-API-KEY");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Credentials: false");
// Handle preflight OPTIONS request
// Browser akan mengirim OPTIONS request sebelum POST/PUT/DELETE jika ada custom headers
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
// Log untuk debugging (opsional, bisa dihapus di production)
if (defined('APP_DEBUG') && APP_DEBUG === true) {
error_log('CORS Handler: ' . $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI']);
}