在php用户身份验证中重定向请求未完成

在php用户身份验证中重定向请求未完成,php,Php,我有一个用于检查管理员身份验证访问的公共功能: class Auth { public static function checkAdminAuthentication() { // initialize the session (if not initialized yet) Session::init(); // self::checkSessionConcurrency(); // if user is not logged in or is not

我有一个用于检查管理员身份验证访问的公共功能:

class Auth {

public static function checkAdminAuthentication()
 {
    // initialize the session (if not initialized yet)
    Session::init();

    // self::checkSessionConcurrency();

    // if user is not logged in or is not an admin (= not role type 7)
    if (!Session::userIsLoggedIn() || Session::get("user_account_type") != 7) {

        // ... then treat user as "not logged in", destroy session, redirect to login page
        Session::destroy();
        header('location: ' . Config::get('URL') . 'admin/login');
        exit();
    }
}
管理控制器类为:

class AdminController extends Controller
{
    /**
     * Construct this object by extending the basic Controller class
     */
    public function __construct()
    {
        parent::__construct();

        // special authentication check for the entire controller: Note the check-ADMIN-authentication!
        // All methods inside this controller are only accessible for admins (= users that have role type 7)
        Auth::checkAdminAuthentication();
    }

    /**
     * This method controls what happens when you move to /admin or /admin/index in your app.
     */
    public function index()
    {

        $this->Language->load('common/dashboard'); 

        $data['header'] = $this->Language->get('heading_title');  

        $this->View->render('admin/index','admin', array(
                'users' => UserModel::getPublicProfilesOfAllUsers(),
                'header' =>  $this->Language->get('heading_title'),

            )
        );
    }

    public function login()
    {

        $this->Language->load('common/dashboard'); 

        $data['header'] = $this->Language->get('heading_title');  

        $this->View->render('admin/login','admin', array(
                'header' =>  $this->Language->get('heading_title'),

            )
        );

    }
}
现在在操作中,我看到错误和重定向未完成:

> “Firefox检测到服务器正在以一种永远无法完成的方式重定向对此地址的请求

此问题有时可能是由于禁用或拒绝接受Cookie造成的。”


如何修复此错误

我认为您必须从该检查中排除
login()
(通过将其移动到其他控制器或其他地方),否则它将无限期重定向:

  • 在调用
    login()
    路由之前,调用
    \u构造
  • \uu construct()
    重定向到
    登录()
  • 在调用
    login()
    路由之前,调用
    \u构造
  • 等等

这是因为您正在使用重定向创建一个无止境的循环。您的构造函数总是调用过程身份验证检查函数,该函数总是重定向到再次执行同一个构造函数。@tereško:您说得对!但更好的方法是什么?如果我只更改模板(即:登录页面-仪表板),而不重定向到另一个页面,这种方式是正确的吗?对于一个非常简单的版本,您可以尝试查看它是如何在中完成的