php – Kohana Auth模块无法登录
发布时间:2020-12-13 22:00:04 所属栏目:PHP教程 来源:网络整理
导读:对于那些熟悉Kohana中的Auth模块的人,我无法登录用户.我可以创建一个用户很好,但显然哈希不匹配.我已经使用提供 MySql架构来创建数据库,而我正在使用模块模型. 这是我创建用户代码: public function user_create() { $user = ORM::factory('user'); $user-u
对于那些熟悉Kohana中的Auth模块的人,我无法登录用户.我可以创建一个用户很好,但显然哈希不匹配.我已经使用提供
MySql架构来创建数据库,而我正在使用模块模型.
这是我创建用户代码: public function user_create() { $user = ORM::factory('user'); $user->username = "user"; $this->auth = Auth::instance(); $user->email = "info@test.com"; $user->password = $this->auth->hash_password('admin'); $user->add(ORM::factory('role','login')); $user->add(ORM::factory('role','admin')); if ($user->save()) { $this->template->title = "user saved"; $this->template->content = "user saved"; } } 它创建一个具有散列密码的用户,并为其提供正确的登录/管理员角色.数据库中的一切看起来都很好.这是我的登录代码.我跳过了检查用户是否已登录的开头部分. $user = $this->input->post('username'); $password = $this->input->post('password'); if(!empty($user)) { $find_user = ORM::factory('user')->where('username',$user)->find(); $username = $find_user->username; $this->auth = Auth::instance(); if($this->auth->login($username,$password)) { $error = "logged in"; } else { $error = "not logged in at all"; } } $this->template->content = new View('admin/login_view'); $this->template->content->user_info = $username . " " . $password; $this->template->title = "Login Admin"; $this->template->content->bind('error',$error); 它总是返回“根本没有登录”.我验证我输入了正确的用户名和密码,但它没有登录.我无法弄清楚原因.我正在使用内置的hash_password函数来创建密码,我已经按照文档,但我无法发现错误.有帮助吗? 解决方法
当您通过__set()方法设置密码时,kohana中的Auth模块会自动散列密码.因此,为了您存储密码,请执行以下操作:
public function user_create() { $user = ORM::factory('user'); $user->username = "user"; $this->auth = Auth::instance(); $user->email = "info@test.com"; $user->password = 'admin'; ... 希望有所帮助.如果你想查看auth模块(models / auth_user.php),你可以看到它记录了密码: public function __set($key,$value) { if ($key === 'password') { // Use Auth to hash the password $value = Auth::instance()->hash_password($value); } parent::__set($key,$value); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |