gpt4 book ai didi

php - Kohana Auth 模块无法登录

转载 作者:可可西里 更新时间:2023-11-01 00:01:39 25 4
gpt4 key购买 nike

对于那些熟悉 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 模块无法登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1751625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com