Files
ctms-yii/cors.php
T
2025-07-13 19:37:38 +08:00

33 lines
1.3 KiB
PHP

<?php
# @Author: 嗨噜客(三亚) <fm453>
# @Date: 2022-05-13T22:20:43+08:00
# @Email: fm453@lukegzs.com
# @Last modified by: fm453
# @Last modified time: 2024-07-01T14:41:31+08:00
# @Copyright: www.hiluker.cn
//通用的header跨域配置函数,允许各应用事先自行编写更适合的方法
if (!function_exists('setheader')) {
function setheader()
{
// 获取当前跨域域名
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
/*
@ORIGINARR 请求来源的定义字段,要求为数组
*/
defined('ORIGINARR') or define('ORIGINARR', []); //如果没有预定义,则默认防
if (in_array($origin, ORIGINARR) || in_array("*", ORIGINARR)) {
// 允许 $originarr 数组内的 域名跨域访问
header('Access-Control-Allow-Origin:' . $origin);
// 响应类型,POST,GET,PUT,等
header('Access-Control-Allow-Methods:POST,GET');
// 带 cookie 的跨域访问
header('Access-Control-Allow-Credentials: true');
// 响应头设置
header('Access-Control-Allow-Headers:x-requested-with,Content-Type,X-CSRF-Token');
}
}
}
// setheader(); //该函数必须在入口页文件中执行才能生效