CorsHeader.php 883 B

1234567891011121314151617181920212223242526272829
  1. <?php
  2. use \Psr\Http\Message\{
  3. ServerRequestInterface,
  4. ResponseInterface
  5. };
  6. class CorsHeader {
  7. public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) {
  8. $method = $_SERVER['REQUEST_METHOD'];
  9. $origin = $_SERVER['HTTP_ORIGIN'];
  10. if(in_array($origin, ['http://localhost:8080', 'https://vv.ecconia.de'])) {
  11. header('Access-Control-Allow-Origin: ' . $origin);
  12. header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
  13. header('Access-Control-Allow-Headers: DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range');
  14. if($method == 'OPTIONS') {
  15. header('Access-Control-Max-Age: 1728000');
  16. header('Content-Type: text/plain charset=UTF-8');
  17. header('Content-Length: 0');
  18. return $response->withStatus(204);
  19. }
  20. }
  21. return $next($request, $response);
  22. }
  23. }