下面的代码php
只允许 URLhttp://example1.com
访问应用程序。如何在 PHP 中只允许访问以下四个 URL?
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
?>