php – Kohana Auth模块无法登录

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Kohana Auth模块无法登录脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_126_2@
对于那些熟悉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);
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – Kohana Auth模块无法登录全部内容,希望文章能够帮你解决php – Kohana Auth模块无法登录所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。