2017-04-13 69 views
0

我再次遇到同樣的問題。標題無法設置 - SlimFramework

舊後here

我有一個角應用和SlimFramework的API連接。

本地它工作正常,但當我發佈到我的網站來的錯誤,我的標題沒有設置。 但API測試工具的信息表明它可以來自* IP。

有人可以幫助我嗎?

這裏有效令牌:基本TyOSZcfBwMC6DR9kbAWeMnPmhF4ohZu2n9LccQEyt6uXNt8PTT

THX

enter image description here

$app = new \Slim\App(["settings" => $config]); 
$container = $app->getContainer(); 
$app->options('/{routes:.+}', function ($request, $response, $args) { 
    return $response; 
}); 
$app->add(function ($req, $res, $next) { 
    $response = $next($req, $res); 
    return $response 
     ->withHeader('Access-Control-Allow-Origin', '*') 
     ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization') 
     ->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, DELETE, PUT'); 
}); 

$container['logger'] = function($c) { 
    $logger = new \Monolog\Logger('my_logger'); 
    $file_handler = new \Monolog\Handler\StreamHandler("../../logs/app.log"); 
    $logger->pushHandler($file_handler); 
    return $logger; 
}; 
$app->get('/token', function ($request, $response){ 
    $db = new DbOperation(); 
    if (!$request->hasHeader('Authorization')) { 
     return $response->withJson([ 
      "success"=> false, 
      "message" => "Header not set.", 
      "textcode"=> "MSG2" 
     ], 401); 
    } 
    $token = $request->getHeader('Authorization'); 

    if($db->checkToken($token[0])){ 
     $user = $db->userInfo($token[0]); 
     if($db->checkActivate($user['auth_user'])){ 
      if($db->checkExpired($user['auth_user'])){ 
       return $response->withJson([ 
        "success"=> false, 
        "message" => "The validity of the login has expired. If you have any questions, please contact the administrator..", 
        "textcode"=> "MSG6" 
       ], 401); 
      } else { 
       return $response->withJson(["success"=> true], 200); 
      } 
     } else { 
      return $response->withJson([ 
       "success"=> false, 
       "message" => "This account has not yet been activated.", 
       "textcode"=> "MSG8" 
      ], 401); 
     } 
    } else { 
     return $response->withJson([ 
      "success"=> false, 
      "message"=>'Invalid token', 
      "textcode"=> "MSG1" 
     ], 403); 
    } 
}); 
+0

請不要將代碼包含爲圖片。它是文本,所以包括它。沒有人會從你的圖像中複製你的代碼並試用它,甚至不會打開圖像。 – jmattheis

+0

用curl執行請求,並且經常發現問題。 –

回答

1

你的基本身份驗證憑據沒有解碼成任何有意義的東西。 PHP往往默默地忽略它認爲格式不正確的授權標頭。嘗試使用類似Basic dGVzdDp0ZXN0的解碼爲test:test的內容。

解決方法爲此已添加到從版本3.5.0開始的Slim。升級Slim安裝可能也有幫助。

+0

Thx。我使用'''Setenvif授權^(。+)$ auth_head = $ 1'''和'''getenv(「auth_head」)來解決它''' – Neonlight