【问题标题】:Kohana/PHP Auth fails while issusing a request through AJAXKohana/PHP Auth 在通过 AJAX 发出请求时失败
【发布时间】:2014-08-18 17:47:51
【问题描述】:

我有一个被所有控制器继承的基类。我在基类中有一个函数,它使用 Auth 确定登录用户的角色。一旦确定了用户角色,就设置了变量 $LoggedIn_role。

此方法在初始页面加载时正确调用,但后来我发出 ajax 调用以检查用户是否仍然登录,当时 Auth::logged_in() 始终返回 0。

我使用的 kohana 版本是 3.3

任何人都可以建议规避此问题的最佳方法是什么。谢谢。

登录 -

if ($ValidObj->check()) {

                    if (Auth::instance()->login($_POST['email'], $_POST['password'],FALSE)) {
                        $this->DoPostLoginJobs();
                    } else {
                        $this->ManageError(Controller_Application::MsgWrongUserCredentials);
                    }
                } else {
                    $this->Form_Errors = $ValidObj->errors('');
                }

退出 -

public function action_logout() {

        $loggedout = Auth::instance()->logout();
        if ($loggedout)
           HTTP::redirect ('/home/'); // redirects to the home page.
    }

在 controller_Application 内部。所有控制器的基类

 public function DetermineUserRole() {
        $this->LoggedIn_Role = Controller_Application::None;
        try {

            if (Auth::instance()->logged_in('freelancer')) {
                $this->LoggedIn_Role = Controller_Application::Freelancer;
                $this->LoggedIn_Id = Auth::instance()->get_user()->pk();
            } else if (Auth::instance()->logged_in('employer')) {
                $this->LoggedIn_Role = Controller_Application::Employer;
                $this->LoggedIn_Id = Auth::instance()->get_user()->pk();
            }
        } catch (Gettrix_Exception $exc) {
            $this->ManageError(Controller_Application::RedirectNonRecoverableError);
        }



public function before() {
if ($this->request->is_ajax()) {

    $this->auto_render = false;

}


 $this->DetermineUserRole();

if($this->auto_render==TRUE){
parent::before();

   $this->template->content = '';
    $this->template->styles = array();
    $this->template->scripts = array();

View::set_global('site_name', 'TheWebTeam');
View::bind_global('Form_Errors', $this->Form_Errors);
View::bind_global('LoggedIn_Role', $this->LoggedIn_Role);
 View::bind_global('LoggedIn_Id', $this->LoggedIn_Id);
View::bind_global('InvitedEmail', $this->InvitedEmail);
View::bind_global('InvitedUniqueID', $this->InvitedUniqueID);
  View::bind_global('scripts', $this->template->scripts); 
   View::bind_global('styles', $this->template->styles);
}

//这是在主页控制器内部,它列出了登录用户的所有作业。

     public function action_joblist()
   {
     echo  Auth::instance()->logged_in() . //The state holds to the initial state, doesn't //change when the user is logged out or logged in.
    }

请注意,action_joblist() 是通过 AJAX/Jquery 调用来调用的。

【问题讨论】:

  • 你能显示一些代码吗?
  • @kingkero 很抱歉,我之前没有注意到您的评论。根据您的指示,我添加了一些代码。如果您需要任何其他详细信息,请告诉我。谢谢。

标签: php ajax kohana kohana-3.3


【解决方案1】:

【讨论】:

    猜你喜欢
    • 2011-10-10
    • 2011-11-08
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2020-11-14
    • 2013-12-26
    • 1970-01-01
    相关资源
    最近更新 更多