【问题标题】:Determine the current environment and requesting origin - need to define variable确定当前环境和请求来源 - 需要定义变量
【发布时间】: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


【解决方案1】:

总体上必须有更好的方法来做到这一点,但问题是:

$httpOrigin = $_SERVER['HTTP_ORIGIN'];

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

【讨论】:

    【解决方案2】:

    我不敢相信我以前没有想到这一点......解决方案:

    /**
    * 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.stie.com'
        )
      , 'qa' = array(
            'http://qa-m.site.com'
          , 'https://qa-m.site.com'
        )
    );
    
    // Determine the current environment and reqesting origin
    switch( $_SERVER['SERVER_NAME'] )
    {
      case 'site.com': // fall-thru
      case 'www.site.com':
        $currentEnv = 'prod';
        break;
      case 'stg.site.com':
        $currentEnv = 'staging';
        break;
      case 'qa.site.com':
        $currentEnv = 'qa';
        break;
      default:
        $currentEnv = 'unknown';
    }
    $httpOrigin = $_SERVER['HTTP_ORIGIN'];
    
    // 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");
    }
    

    解决了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 2012-03-10
      • 2022-11-03
      • 1970-01-01
      • 2014-12-30
      • 1970-01-01
      相关资源
      最近更新 更多