2010-03-30 30 views

回答

12

当用户选中您网站上的“记住我”框时,会使用它。令牌为用户生成并存储在user_tokens表中。

如果你看一下在_login功能Kohana_Auth_ORM类,你可以看到它是如何创建的:

if ($remember === TRUE) 
    { 
     // Create a new autologin token 
     $token = ORM::factory('user_token'); 

     // Set token data 
     $token->user_id = $user->id; 
     $token->expires = time() + $this->config['lifetime']; 
     $token->save(); 

     // Set the autologin cookie 
     cookie::set('authautologin', $token->token, $this->config['lifetime']); 
    } 

它是由AUTO_LOGIN使用()功能也在Kohana_Auth_ORM类:

/** 
* Logs a user in, based on the authautologin cookie. 
* 
* @return boolean 
*/ 
public function auto_login() 
{ 
    if ($token = cookie::get('authautologin')) 
    { 
     // Load the token and user 
     $token = ORM::factory('user_token', array('token' => $token)); 

     if ($token->loaded() AND $token->user->loaded()) 
     { 
      if ($token->user_agent === sha1(Request::$user_agent)) 
      { 
       // Save the token to create a new unique token 
       $token->save(); 

       // Set the new token 
       cookie::set('authautologin', $token->token, $token->expires - time()); 

       // Complete the login with the found data 
       $this->complete_login($token->user); 

       // Automatic login was successful 
       return TRUE; 
      } 

      // Token is invalid 
      $token->delete(); 
     } 
    } 

    return FALSE; 
} 

您需要在您的授权控制器内正确使用此功能,由您决定。我是比较新的Kohana的,但我进行简单的检查,如果他们去登录表单,并已登录,也可以自动登录到重定向用户:

if (Auth::instance()->logged_in() || Auth::instance()->auto_login()) 
    Request::instance()->redirect('auth/'); 

代码为验证模块ISN”太难理解了。如果您是Kohana的新手,那么了解ORM模块的工作原理是一个很好的起点。

+0

嗨,Brian Riehman,谢谢你的重新申请。是的,我是Kohana框架的新手。 – Asif 2010-03-31 04:54:49