【问题标题】:PHP Session based authentication issue with blocking api calls基于 PHP 会话的身份验证问题与阻止 api 调用
【发布时间】:2026-01-29 07:35:01
【问题描述】:

在我的瘦应用程序上,我使用基于 cookie 的身份验证,在成功的身份验证时我设置了 $_SESSION['id'],所以我可以知道用户已通过身份验证,现在我想在任何 API 调用之前检查用户是否已通过身份验证,但我不想检查用户是否正在调用 post 方法进行身份验证。下面是我的 index.php,你可以看到我正在检查会话,如果没有设置 cookie,我只会返回 http 错误。但是在这种方式下,我被阻止进行身份验证调用,这意味着我无法登录到应用程序。禁用对身份验证后调用检查的最佳方法是什么?

<?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 的样子,我这样做是为了 Rob 的回答。

<?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();

【问题讨论】:

    标签: php session authentication slim


    【解决方案1】:

    您可能希望将Middleware 用于您的路由并创建一个不需要身份验证的公共路由列表。 注意:这只会使用 url 结构中的第一个路由。

        $app = new Slim\App;
    
    // add middleware to routes
    $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'])) {
            return $response
                    ->withStatus(401)
                    ->write('You are not authenticated, please authenticate!');
        }
    
        // public route or valid user
        return $next($request,$response);
    });
    
    $app->get('/authenticate',function($request,$response) {
        return $response->write('Login');
    });
    
    $app->get('/admin',function($request,$response) {
        return $response->write('Admin page');
    });
    
    $app->run();
    

    【讨论】:

    • 谢谢你,先生,我已经用我所做的更改更新了我的问题,你能再看看它是否可以。
    • @SuperMario'sYoshi 我更新了我的答案。出于一致性目的,我会使用 Slim 的 withStatus() 和 write() 响应方法。如果你决定使用你所拥有的,那么正确的 http 状态应该是 401。