【发布时间】:2013-11-16 19:23:27
【问题描述】:
除了最后一个变量 $currentEnv = '???' 之外,我几乎完成了所有代码.如何使用这种语法检测当前环境??
/**
* CORS Implementation to support AJAX requests from mobile site
* @see http://en.wikipedia.org/wiki/Cross-origin_resource_sharing
*/
// Define allowed origins per environment
$allowedOrigins = array(
'prod' = array(
'http://m.site.com'
, 'https://m.site.com'
)
, 'staging' = array(
'http://stg-m.site.com'
, 'https://stg-m.site.com'
)
, 'qa' = array(
'http://qa-m.site.com'
, 'https://qa-m.site.com'
)
);
// Determine the current environment and requesting origin
$httpOrigin = $_SERVER['HTTP_ORIGIN'];
$currentEnv = "prod"; **// Something that determines the environment...**
// Allow only if all points match
if ( isset($allowedOrigins[$currentEnv])
&& in_array($httpOrigin, $allowedOrigins[$currentEnv])
){
header("Access-Control-Allow-Origin: $httpOrigin");
header("Access-Control-Allow-Methods: POST, GET");
}
【问题讨论】:
-
WTH 是当前环境、开发、生产?谁定义了这个,它应该来自哪里?
-
我正在配置它,以便我只需要一个函数来包含在三个代码库中,它将在标题中返回原点。
-
我可以简单地将源定义为'site.com',但我想使用一个 var 来检测环境并根据哪个主机创建它
标签: javascript php http cors same-origin-policy