javascript - 判断当前环境和请求来源——需要定义变量

标签 javascript php http cors same-origin-policy

除了最后一个变量 $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");
    }

最佳答案

总的来说应该是更好的方法,但问题是:

$httpOrigin = $_SERVER['HTTP_ORIGIN'];

foreach($allowedOrigins as $env => $url) {
    if(in_array($httpOrigin, $url)) {
        $currentEnv = $env;
        break;
    }
}

关于javascript - 判断当前环境和请求来源——需要定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19797137/

相关文章:

javascript - 如何序列化Json javascript

php - css中高度的条件语句

php - 服务器可以阻止 curl 请求吗?

ruby - 是否有比 Net::HTTP 更用户友好的替代方案来与 REST API 交互?

api - Rest api design : POST to create with duplicate data, would-be IntegrityError/500,什么是正确的?

javascript - angularJS - 迭代 JSON 对象

javascript - 如何将现有的 pdf 文件添加到我的 React.js 页面?

javascript - 使用 ember 添加 Dropbox 选择器

PHP变量变量困惑

php - WPGlobus - 语言 http 重定向错误