本文目录导读:

- 最基础的 CORS 配置
- 指定特定域名(生产环境推荐)
- 处理预检请求(OPTIONS 请求)
- 完整的 CORS 中间件函数
- 在框架中的配置(以 Laravel 为例)
- 使用 .htaccess 配置(Apache)
- Nginx 配置
- 注意事项
在 PHP 中配置 CORS(跨域资源共享)主要有以下几种方式,我会从简单到复杂逐一说明。
最基础的 CORS 配置
在 PHP 文件头部添加:
<?php
// 允许所有来源
header('Access-Control-Allow-Origin: *');
// 允许的请求方法
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
// 允许的请求头
header('Access-Control-Allow-Headers: Content-Type, Authorization');
?>
指定特定域名(生产环境推荐)
<?php
// 允许特定域名
$allowed_origins = [
'http://yourdomain.com',
'https://yourdomain.com',
'http://localhost:3000'
];
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
if (in_array($origin, $allowed_origins)) {
header("Access-Control-Allow-Origin: $origin");
header('Access-Control-Allow-Credentials: true');
}
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Access-Control-Max-Age: 86400'); // 预检请求缓存时间(秒)
?>
处理预检请求(OPTIONS 请求)
<?php
// 处理预检请求
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Access-Control-Max-Age: 86400');
header('Content-Length: 0');
header('Content-Type: text/plain');
// 返回 200 并退出
exit(0);
}
// 正常请求处理...
?>
完整的 CORS 中间件函数
<?php
function cors() {
// 支持的来源
$origin = $_SERVER['HTTP_ORIGIN'] ?? '*';
// 允许的来源列表
$allow_origins = [
'http://localhost:3000',
'https://yourdomain.com',
'http://yourdomain.com'
];
if (in_array($origin, $allow_origins)) {
header("Access-Control-Allow-Origin: $origin");
header('Access-Control-Allow-Credentials: true');
} else {
header('Access-Control-Allow-Origin: *');
}
// 允许的请求方法
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
// 允许的请求头
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
// 预检请求缓存时间
header('Access-Control-Max-Age: 86400');
// 处理预检请求
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('HTTP/1.1 204 No Content');
exit();
}
}
// 使用
cors();
// 后续业务逻辑...
?>
在框架中的配置(以 Laravel 为例)
// Laravel 中推荐使用中间件
namespace App\Http\Middleware;
use Closure;
class Cors
{
public function handle($request, Closure $next)
{
$response = $next($request);
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if ($request->isMethod('OPTIONS')) {
$response->setStatusCode(204);
}
return $response;
}
}
使用 .htaccess 配置(Apache)
# .htaccess 文件 Header set Access-Control-Allow-Origin "*" Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" Header set Access-Control-Allow-Headers "Content-Type, Authorization"
Nginx 配置
# nginx.conf 或站点配置
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization';
add_header 'Access-Control-Max-Age' 86400;
return 204;
}
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization';
}
注意事项
- 安全性:生产环境使用 允许所有来源时要谨慎,最好指定具体域名
- Credentials:使用
Access-Control-Allow-Credentials: true时,Access-Control-Allow-Origin不能为 - 预检请求:复杂请求(如带自定义头的请求)会先发 OPTIONS 预检请求
- 缓存:合理设置
Access-Control-Max-Age可以减少预检请求次数
选择适合你项目的配置方式,开发环境可以用 ,生产环境建议限制具体域名。