【问题标题】:PHP REST Tonic and authenticationPHP REST Tonic 和身份验证
【发布时间】:2012-06-05 17:00:33
【问题描述】:

我正在使用 PHP Tonic 框架。 我以这个例子为起点:GitHub link

我想添加一个基于 cookie 的身份验证方法。这是正确的解决方案吗? 前一个基于查询字符串中的令牌变量,例如: /serviceName?token=XXXXXXXxxxXXX**& 令牌有效期为 10 秒。

当然,这不是一个合适的解决方案。我不能使用浏览器缓存,因为每次调用都会更改令牌。 想法?

【问题讨论】:

    标签: php authentication rest cookies


    【解决方案1】:

    一种可能的解决方案是使用 HTTP 标头。

    $urlToCall = '<get the link>';
    $credentials =  Crypt::createToken();
    $reqHeaders = apache_request_headers();
    
    // Configuring curl options extra:CURLOPT_HEADER => 1,
    $options = array(
        CURLOPT_RETURNTRANSFER => true, 
        CURLOPT_HTTPHEADER => array('Authorization: ' . $credentials)
    );
    
    $ch = curl_init($urlToCall);
    curl_setopt_array($ch, $options);
    $response = curl_exec($ch);
    
    /*
    //$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    //$header = substr($response, 0, $header_size);
    //$body = substr($response, $header_size);
    
    if (curl_errno($ch)) {
        print curl_error($ch);
    } else {
        list($headers, $content) = explode("\r\n\r\n", $response, 2);
        foreach (explode("\r\n", $headers) as $hdr) {
            header($hdr);   
        //echo $hdr . '<br>';
        //print_r($hdr);
        }
        echo $content;
    }
    */
    
    if (curl_errno($ch)) {
        print curl_error($ch);
    } else {
        header("Accept: " . $reqHeaders['Accept']);
        $finfo = new finfo(FILEINFO_MIME);
        header("Content-Type: " . $finfo->buffer($response));
        print $response;
    }
    curl_close($ch);
    

    【讨论】:

      猜你喜欢
      • 2011-11-01
      • 1970-01-01
      • 2011-07-15
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      • 2015-04-14
      • 2015-04-15
      相关资源
      最近更新 更多