Current Location: Home> Latest Articles> Set cross-domain header (CORS) response: Usage of Access-Control-Allow-Origin

Set cross-domain header (CORS) response: Usage of Access-Control-Allow-Origin

M66 2025-05-28

Cross-domain problems are a very common problem in web development. Especially in applications with separate front-end and back-end applications, the front-end may need to access resources under different domain names, and the browser will block cross-domain requests by default for security reasons. At this time, the server needs to set HTTP headers to allow cross-domain requests. In PHP, we can use the header() function to set cross-domain related header information.

1. Cross-domain problems and solutions

Cross-domain resource sharing (CORS) is a mechanism that solves cross-domain problems by allowing browsers to initiate requests to different domains. CORS mainly relies on HTTP headers, especially Access-Control-Allow-Origin . When a browser initiates a request to a server in a different domain, the server needs to include Access-Control-Allow-Origin in the response header to explicitly allow cross-domain requests.

2. Detailed explanation of Access-Control-Allow-Origin

Access-Control-Allow-Origin is a key header in the CORS standard, which determines which domain names can access resources. Its value can be in the following cases:

  • * : means that all domain names can access the resource.

  • http://m66.net : Only this specified domain name is allowed to access resources.

  • null : means that cross-domain requests are not allowed.

For example, when we want to allow the domain name http://m66.net to make cross-domain requests, we need to set the corresponding header in the PHP script.

3. Set up Access-Control-Allow-Origin using PHP

In PHP, setting HTTP header information is implemented through the header() function. To allow cross-domain requests, the most basic step is to set Access-Control-Allow-Origin using header() .

Sample code:

 <?php
// Allow fromhttp://m66.netCross-domain requests
header("Access-Control-Allow-Origin: http://m66.net");

// If you need to support otherHTTPmethod(likePOST、PUT、DELETEwait),This can be set:
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");

// like果需要允许携带cookie,You need to set the following header
header("Access-Control-Allow-Credentials: true");

// Set the allowed request header
header("Access-Control-Allow-Headers: Content-Type, Authorization");

// In practical application,You can determine the source domain name of the request,To dynamically set cross-domain headers
$allowedOrigin = "http://m66.net";
if ($_SERVER['HTTP_ORIGIN'] === $allowedOrigin) {
    header("Access-Control-Allow-Origin: $allowedOrigin");
}
?>

explain:

  • Access-Control-Allow-Origin : Allows the specified domain name to make cross-domain requests. If you want to allow multiple domain names, you can dynamically determine the source of the request and set different domain names.

  • Access-Control-Allow-Methods : Allowed HTTP methods (such as GET, POST, etc.).

  • Access-Control-Allow-Credentials : If you need to send a cookie, you need to set the header to true .

  • Access-Control-Allow-Headers : Allowed custom request headers (such as Content-Type or Authorization ).

4. Pre-flight request (OPTIONS)

When the browser initiates some complex cross-domain requests (such as with custom headers or using PUT, DELETE methods, etc.), the browser will first send an OPTIONS request, called a preflight request. This request is to check whether the server allows cross-domain requests. In the response to the preflight request, the server also needs to return the corresponding CORS header.

For example:

 <?php
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    // Process pre-flight requests
    header("Access-Control-Allow-Origin: http://m66.net");
    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
    header("Access-Control-Allow-Headers: Content-Type, Authorization");
    header("Access-Control-Allow-Credentials: true");
    exit();
}
?>

explain:

  • OPTIONS requests do not need to return actual data, but the CORS header needs to be set correctly to tell the browser server whether cross-domain requests are allowed.

5. Summary

By using PHP's header() function, we can easily set cross-domain related HTTP headers, especially Access-Control-Allow-Origin , to allow cross-domain requests for specified domain names. According to actual needs, we can flexibly configure CORS policies to ensure a balance between security and functionality.

Cross-domain resource sharing is a problem that cannot be ignored in web applications. Mastering how to configure CORS headers in PHP can help you solve cross-domain problems and improve the accessibility and scalability of your application.