duandai0373 2014-06-18 11: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 22: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条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加