O php
código abaixo permite que apenas a URL http://example1.com
tenha acesso à aplicação. Como posso permitir acesso apenas às quatro URLs abaixo em PHP?
http://example1.com
http://example2.com
http://example3.com
http://example4.com
<?php
error_reporting(0);
header('Access-Control-Allow-Origin: http://example1.com');
header('Access-Control-Allow-Methods: POST');
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
$allowedOrigin = 'http://example1.com'; // Your expected origin
if ($_SERVER['HTTP_ORIGIN'] !== $allowedOrigin) {
//echo "Access Denied.";
$return_arr = array("msg"=>"Access Denied");
echo json_encode($return_arr);
exit();
}
$validReferer = 'http://example1.com/'; // Change to your actual domain! Include trailing slash if the AJAX call comes from a subfolder
if (strpos($_SERVER['HTTP_REFERER'], $validReferer) !== 0) {
// echo "Access Denied (Invalid Referer).";
$return_arr = array("msg"=>"Access Denied");
echo json_encode($return_arr);
exit();
}
// begin insert records into database
?>