如何在Web服务器上启用授权请求?

时间:2016-05-09 10:23:04

标签: php .htaccess

我是php和MySQL的新手。我创建了Restful API,我需要将我的应用程序与服务器连接起来,一切都在localhost上工作正常,但是当我转移到服务器并在线托管文件时,我无法在那里工作。

我正在使用Slim库来构建Restful API。

这里我正在检查Http请求中是否存在授权密钥,并检查它是否有效:

function authenticate(\Slim\Route $route) {
// Getting request headers
$headers = apache_request_headers();
$response = array();
$app = \Slim\Slim::getInstance();

// Verifying Authorization Header
if (isset($headers['Authorization'])) {
    $db = new DbHandler();

    // get the api key
    $api_key = $headers['Authorization'];
    // validating api key
    if (!$db->isValidApiKey($api_key)) {
        // api key is not present in users table
        $response["error"] = true;
        $response["message"] = "Access Denied. Invalid Api key";
        echoResponse(401, $response);
        $app->stop();
    } else {
        global $user_id;
        // get user primary key id
        $user_id = $db->getUserId($api_key);

    }
} else {
    // api key is missing in header
    $response["error"] = true;
    $response["message"] = "Api key is misssing";
    echoResponse(400, $response);
    $app->stop();
}
}

即使我在Http请求中放置了授权密钥,我也收到Api密钥丢失的消息。

这是我的htaccess文件:

    RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

0 个答案:

没有答案
相关问题