蛋糕php中的未定义索引X-Api-Key

时间:2018-10-15 09:42:03

标签: php cakephp http-headers cakephp-2.x

我正在尝试以json格式发布一些数据,并传递“ X-Api-Key”作为标题键。但是我的代码始终会给出未定义索引x-api-key的通知。并且没有数据被存储。 我正在使用蛋糕php 2.x 这是我的php代码

    public function checkXAPI() {

        $headers = apache_request_headers();
        //X-Api-key unidentified
        if($headers['X-Api-Key'] == 'AB5433GMDF657VBB'){
            return true;
        } else {
            return true;
        }
    }



public function beforeFilter(){

    $this->Auth->Allow(array('checkXAPI','Registerowner'));

    $headersNotAllowed = array('login', 'checkXAPI', 'register');

    $noSecurityAllowed = array('register');
    if(!in_array($this->request->action, $noSecurityAllowed)){
        $checkXAPI = $this->checkXAPI();
        if (!$checkXAPI) {
            $message = array(
                'status' => false,
                'message' => __('wrong X-API')
            );
            echo json_encode($message);
            exit;
        } else {
            $headers = apache_request_headers();
            if(!in_array($this->request->action, $headersNotAllowed)){

                if (isset($headers['Id'])) {
                    $checkUserToken = $this->checkUserToken();
                    if (!$checkUserToken) {
                        $message = array(
                            'status' => false,
                            'message' => __('wrong User ID and User token combination')
                        );
                        echo json_encode($message);
                        exit;
                    }
                }  
            }  
    }


    }

} 

1 个答案:

答案 0 :(得分:2)

改为使用$this->request->header('X-Api-Key')

public function checkXAPI() {
    return $this->request->header('X-Api-Key') === 'AB5433GMDF657VBB';
}