0
내 슬림 앱에서 쿠키 기반 인증을 사용하여 성공적인 인증에서 $ _SESSION [ 'id']을 (를) 설정 했으므로 사용자가 인증되었음을 알게되었습니다. 이제는 API 호출 전에 필요합니다. 사용자가 인증되었는지 확인하려면 사용자가 인증하기 위해 게시 방법을 호출하는지 확인하고 싶지 않습니다. 아래는 내 index.php이며 거기에서 볼 수 있듯이 세션을 확인 중이며 쿠키가 설정되지 않은 경우 http 오류 만 반환합니다. 하지만 이렇게하면 인증 요청을받지 못하게되고 앱에 로그인 할 수 없다는 뜻입니다. 게시물 인증 호출에 대한 확인을 비활성화하는 가장 좋은 방법은 무엇입니까?API 호출을 차단하는 PHP 세션 기반 인증 문제
<?php
require 'vendor/autoload.php';
session_start();
if (empty($_SESSION['id'])) {
http_response_code(423);
exit('You are not authenticated, please authenticate!');
}
$app = new \Slim\App;
require_once 'rest/authentication/authentication.php';
require_once 'rest/users/users.php';
require_once 'rest/control-groups/controlGroups.php';
require_once 'rest/clients/clients.php';
require_once 'rest/attendants/attendants.php';
require_once 'rest/calendar/caringCalendar.php';
$app->run();
편집 :
이 내의 index.php가 보인다 내가 롭의 대답에 그것을 들으 한 방법이다.
<?php
require 'vendor/autoload.php';
session_start();
$app = new \Slim\App;
$app->add(function($request,$response,$next) {
// public route array
$public = array('authenticate');
// get the first route in the url
$uri = $request->getUri();
$path = explode('/', $uri->getPath());
$requestRoute = $path[1];
// if the first route in the url is not in the public array, check for logged in user
if (!in_array($requestRoute,$public) && empty($_SESSION['id'])) {
http_response_code(423);
exit('You are not authenticated, plase authenticate!');
}
// public route or valid user
return $next($request, $response);
});
require_once 'rest/authentication/authentication.php';
require_once 'rest/users/users.php';
require_once 'rest/control-groups/controlGroups.php';
require_once 'rest/clients/clients.php';
require_once 'rest/attendants/attendants.php';
require_once 'rest/calendar/caringCalendar.php';
$app->run();
고맙습니다. 제가 변경 한 내용으로 제 질문을 업데이트했습니다. 다른 모양을하고 확인해 볼 수 있습니까? –
@ SuperMario'sYoshi 답변을 업데이트했습니다. Slim의 응답 메소드 withStatus()와 write()를 사용하여 일관성을 유지합니다. 가지고있는 것을 사용하기로 결정했다면 적절한 http 상태는 401이됩니다. – Rob