Reputation: 55
I am created a PHP RESP API application. I need to access this application from another domain using its API. Is it possible to specify cors domain in Header
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
...
The above code allows cors from every domain.But i need to allow cors from a specific domain only.
Upvotes: 2
Views: 6460
Reputation:
This may not answer your immediate question, but personally I use the below to make sure content gets out only if on the proper host.
$host = 'example.com';
if ($_SERVER['HTTP_HOST'] != $host) {
header("Location: http://$host");
exit;
}
In .htaccess I set Header set X-Frame-Options "SAMEORIGIN"
to further limit access.
Upvotes: 1
Reputation: 102
try below code:
<?php
// Cross-Origin Resource Sharing Header
header('Access-Control-Allow-Origin: http://base.com');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept');
?>
Upvotes: 6
Reputation: 2147
The solution to this issue is to use the $_SERVER['HTTP_ORIGIN'] variable to determine whether the request has come from an allowed domain. It has then set this:
try below code:
header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
Upvotes: -2