关闭
duandai0373 2014-06-18 03:22
浏览 110
已采纳

Slim php,停止,在小型中间件中发送403响应状态代码后死亡

I created a small middleware function for slim php framework that checks if the user is authenticated or not, like so.

function authenticate($app) {
    return function() use ($app) {
        if (!isset($_SESSION['user'])) {
            $response = array();
            array_push($response, array(
                    'error' => true,
                    'message' => 'You are not logged in.'
                )
            );
            echoRes(403, $response);
        }
    };
}

What happens is that if I tried to insert it in a route like this:

$app->get('/', authenticate($app), function() use ($app){
     echoRes(200, 'hello world!');
});

The echoRes function

function echoRes($code, $response) {
    $app = \Slim\Slim::getInstance();
    $app->status($code);
    $app->contentType('application/json');
    echo json_encode($response);
}

What happens is that it will continue to give me a status code of 200 even when not authenticated, even I kill it using die();

function authenticate($app) {
    return function() use ($app) {
        if (!isset($_SESSION['user'])) {
            $response = array();
            array_push($response, array(
                    'error' => true,
                    'message' => 'You are not logged in.'
                )
            );
            echoRes(403, $response);
            die();
        }
    };
}

展开全部

  • 写回答

2条回答 默认 最新

  • dt20081409 2014-06-19 14:24
    关注

    I use $app->notfound() or $app->halt(403) to halt execution. There is no need to set the status code as it is set by these functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部