1
0
plweb/functions.php
Anna Christina Naß ce5baaac35 Erster Commit
2025-07-07 16:25:22 +02:00

43 lines
1.3 KiB
PHP

<?php
/**
* An example CORS-compliant method. It will allow any GET, POST, or OPTIONS requests from any
* origin.
*
* In a production environment, you probably want to be more restrictive, but this gives you
* the general idea of what is involved. For the nitty-gritty low-down, read:
*
* - https://developer.mozilla.org/en/HTTP_access_control
* - https://fetch.spec.whatwg.org/#http-cors-protocol
*
*/
function cors() {
// Allow from any origin
header("Access-Control-Allow-Origin: *");
header('Access-Control-Max-Age: 86400'); // cache for 1 day
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Request-Headers: content-type");
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
// header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
// header("Access-Control-Request-Headers: content-type");
exit(0);
}
}
function return_success() {
return json_encode(['status' => 'success', 'message' => 'OK']);
}
function return_error($code, $msg) {
http_response_code($code);
return json_encode(['status' => 'error', 'message' => $msg]);
}
function twig_error($twig, $code, $msg) {
http_response_code($code);
return $twig->render('error.html.twig', [ 'error' => $msg ]);
}
?>