現在の言語: 日本語 |
HTTP_ORIGIN |
$_SERVER |
// 許可するオリジンリスト
$allowed_origins = ['https://nekofes.github.io', 'https://www.nekofes.github.io'];
// リクエストのOriginヘッダーを取得
$origin = $_SERVER['HTTP_ORIGIN'] ?? "";
// Originが許可リストに含まれているか確認
if (in_array($origin, $allowed_origins))
{
// 許可する場合、Access-Control-Allow-Originヘッダーをセット
header('Access-Control-Allow-Origin: ' . $origin);
// その他のCORS関連ヘッダーも必要に応じてセット
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
}
else
{
// 許可しない場合の処理(例: エラーログを出力するなど)
// ブラウザはデフォルトでアクセスをブロックする
}
// OPTIONSメソッドに対するプリフライトリクエストの処理
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
exit; // OPTIONSリクエストでは実際の処理は不要
}
// ここから通常のリクエスト処理...
// Allowed origin list
$allowed_origins = ['https://nekofes.github.io', 'https://www.nekofes.github.io'];
// Get the request's Origin header
$origin = $_SERVER['HTTP_ORIGIN'] ?? "";
// Check if the origin is in the allowed list
if (in_array($origin, $allowed_origins))
{
// If allowed, set the Access-Control-Allow-Origin header
header('Access-Control-Allow-Origin: ' . $origin);
// Set other CORS-related headers as needed
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
}
else
{
// Handling the case where the request is not allowed (e.g., outputting an error log)
/ Browsers block access by default.
}
// Process a preflight request for the OPTIONS method.
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
exit; // No actual processing is required for OPTIONS requests.
}
// Normal request processing begins...
| 送信元(自分のサーバー) | 送信先 | HTTP_ORIGINの結果 | 備考 |
| https://aaa.com/index.html | https://aaa.com/test.html | null または空文字列 | 同じウェブサーバー |
| https://aaa.com/index.html | https://bbb.com/index.html | https://bbb.com | 異なるウェブサーバー |
| https://localhost/index.html | https://localhost/index.html | null または空文字列 | 同じサーバー |
| https://localhost/index.html | https://localhost/test.html | null または空文字列 | 同じサーバー |
| 著作権情報 |
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |