doutou19761022 2015-11-16 07:03
浏览 21
已采纳

如何在Fat-Free Framework中显示自定义401页面?

I'm new to F3 and I've just implemented a form-based login system (using the Auth plugin). It works, but my not-logged-in check looks like this:

if (!$f3->get('SESSION.user')) $f3->reroute('/login');

The example I was referring to, did this, which seems more correct:

if (!$f3->get('SESSION.user')) $f3->error(401);

However, this 401 error shows a simple error page. I want it to send back the login form (which is at /login) along with an error saying "You must be logged in" and I want this response to be a 401 instead of a 403 or 200.

Am I right in expecting this behaviour or is a 401 only valid for HTTP Basic Auth and not custom form-based auth?

  • 写回答

1条回答 默认 最新

  • doujian7132 2015-11-16 08:28
    关注

    You can customize the error response with the ONERROR hook.

    So in your case, you could do something like this:

    $f3->ONERROR=function($f3) {
      if ($f3->get('ERROR.code')==401) {
        // custom behaviour on 401
        echo \Template::instance()->render('error-401.htm');
      } else
        // default behaviour otherwise
        return FALSE;
    };
    
    <!-- error-401.htm -->
    <h1>This page requires authorization</h1>
    <a href="/login?path={{ @PATH }}">Click here to sign in</a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了