header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");
把上面代码加入到public/index.php入口文件,生产环境把header("Access-Control-Allow-Origin:*");后面星号换成允许的域名,安全起见
<?php header("Access-Control-Allow-Origin:*"); header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE"); header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding"); // [ 应用入口文件 ] // 定义应用目录 define('APP_PATH', __DIR__ . '/../application/'); // 判断是否安装 if (!is_file(APP_PATH . 'admin/command/Install/install.lock')) { header("location:./install.php"); exit; } // 加载框架引导文件 require __DIR__ . '/../thinkphp/start.php';
本文介绍了在PHP中设置跨域访问控制的方法,强调了在生产环境中将`Access-Control-Allow-Origin`的通配符替换为特定域名的重要性,以确保应用的安全性。同时,文章还提及了其他相关HTTP头部的配置,如允许的请求方法和头部,以及在未安装状态下重定向到安装页面的逻辑。
4330

被折叠的 条评论
为什么被折叠?



