1

我正在尝试学习 Kohana 的 Auth 模块,但登录方法总是返回 false。

控制器:

<?php defined('SYSPATH') OR die('No Direct Script Access');
class Controller_Auth extends Controller {
    public function action_index() {
        if($_POST) {
            $this->login();
        }

        $this->template = View::factory('login');
        echo $this->template;
    }

    private function login() {
    $user = ORM::factory('user');

        $data = array('username' => 'wilson', 'password' => '123');
        if(!$user->login($data)) {
            echo 'FAILED!';
        }
    }

    private function logout() {

    }
}    
?>

模型:

<?php defined('SYSPATH')  or die('No direct script access.');
class Model_User extends Model_Auth_User {
}
?>
4

2 回答 2

1

你这样做是错的。

要登录用户,请执行以下操作:

$auth = Auth::instance();
if ($auth->login($_POST['username'], $_POST['password']))
{
      echo 'hello, '.$auth->$_POST['username'];
}
else
{
      echo 'login failed!';
}

此外,取消注释应用程序引导程序中的 auth 模块行:

'auth'       => MODPATH.'auth',       // Basic authentication
于 2010-06-10T18:48:15.643 回答
1

我没有向用户添加规则。欲了解更多信息,[请参阅此链接][1]。

谢谢你们,对此感到抱歉:)

http://webcache.googleusercontent.com/search?q=cache:kXKFWswjDogJ:kerkness.ca/kowiki/doku.php%3Fid%3Dusing_the_auth_module_in_your_controllers+&cd=1&hl=en&ct=clnk&gl=us

public function action_signin()
{
    #If user already signed-in
    if(Auth::instance()->logged_in()!= 0){
        #redirect to the user account
        Request::instance()->redirect('account/myaccount');     
    }

    $content = $this->template->content = View::factory('signin');  

    #If there is a post and $_POST is not empty
    if ($_POST)
    {
        #Instantiate a new user
        $user = ORM::factory('user');

        #Check Auth
        $status = $user->login($_POST);

        #If the post data validates using the rules setup in the user model
        if ($status)
        {       
            #redirect to the user account
            Request::instance()->redirect('account/myaccount');
        }else
        {
                            #Get errors for display in view
            $content->errors = $_POST->errors('signin');
        }

    }
}
于 2010-06-10T20:43:15.287 回答