Keywords: CORS | Cross-Domain Requests | XMLHttpRequest | Security Configuration | PHP Server
Abstract: This paper provides an in-depth examination of the common 'Origin is not allowed by Access-Control-Allow-Origin' error in XMLHttpRequest cross-domain requests. It thoroughly explains the CORS mechanism's working principles, security risks, and multiple resolution strategies. Through PHP and Apache configuration examples, it demonstrates proper server-side CORS header settings, including both wildcard and domain whitelist approaches, while discussing key technical aspects such as preflight requests and security best practices.
Understanding CORS Error Mechanisms
When web applications attempt to make XMLHttpRequest or Fetch API requests to servers with different origins, browsers enforce the same-origin policy. If the target server does not explicitly permit cross-origin access, the browser blocks the request and throws the 'Origin is not allowed by Access-Control-Allow-Origin' error. This security mechanism is designed to prevent malicious websites from stealing user data.
Server-Side Resolution Methods
For PHP servers, the simplest solution involves adding the Access-Control-Allow-Origin header to responses. The following code demonstrates how to allow cross-origin access from all domains:
<?php
header('Access-Control-Allow-Origin: *');
?>
While this approach is convenient, it poses significant security risks by completely disabling CORS protection. Attackers could potentially exploit this configuration for cross-site request forgery (CSRF) attacks.
Security Best Practices
In production environments, implementing domain whitelisting is recommended to permit cross-origin access only from specific trusted domains. The following PHP code illustrates domain whitelist implementation:
<?php
$allowed_origins = array(
'http://example.com',
'http://localhost:8888',
'https://trusted-domain.com'
);
if (in_array($_SERVER['HTTP_ORIGIN'], $allowed_origins)) {
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
}
?>
Apache Server Configuration
For Apache servers, CORS headers can be added in .htaccess files or virtual host configurations. The following configuration permits cross-origin access from all domains:
Header set Access-Control-Allow-Origin "*"
A more secure approach involves restricting access to specific domains:
Header set Access-Control-Allow-Origin "http://example.com"
Preflight Request Handling
For non-simple requests (those containing custom headers or using specific HTTP methods), browsers first send OPTIONS preflight requests. Servers must properly handle these requests:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
header('Access-Control-Allow-Origin: http://example.com');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
exit(0);
}
?>
Practical Application Scenarios
In Sencha Touch and PhoneGap hybrid application development, frequent requests from local servers to remote APIs are common. Proper CORS header configuration ensures application functionality while maintaining appropriate security levels. Developers should select suitable CORS strategies based on specific requirements, balancing functional needs with security considerations.