gpt4 book ai didi

php - 使用用户名和/或电子邮件登录

转载 作者:行者123 更新时间:2023-11-29 22:45:04 24 4
gpt4 key购买 nike

我已经在一个问题上苦苦挣扎了很多天,所以我终于决定寻求一些帮助。我想实现一种登录,在一个输入字段中接受用户的电子邮件或用户名。我的仅用户名登录工作正常,但我尝试在电子邮件登录中工作,但根本没有成功。

我在 mysql 中的表字段是“用户名”和“电子邮件”,如果有人可以为我提供一些帮助,那就太好了!非常感谢!

这是我尝试过的代码:

public function find($user = null) {
if($user) {
$field = (is_numeric($user)) ? 'id' : 'username';
$data = $this->_db->get('users', array($field, '=', $user));

if($data->count()) {
$this->_data = $data->first();
return true;
}
}
else if($user) {
$field = (is_numeric($user)) ? 'id' : 'email';
$data = $this->_db->get('users', array($field, '=', $user));

if($data->count()) {
$this->_data = $data->first();
return true;
}
}
return false;
}

public function login($username = null, $useremail = null, $password = null, $remember = false) {

if(!$username && !$password || !$useremail && !$password && $this->exists()) {
Session::put($this->_sessionName, $this->data()->id);
} else {
$user = $this->find($username);
$useremail = $this->find($email);
if($user or $useremail) {
if($this->data()->password === Hash::make($password, $this->data()->salt)) {
Session::put($this->_sessionName, $this->data()->id);
if($remember) {
$hash = Hash::unique();
$hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id));
if(!$hashCheck->count()) {
$this->_db->insert('users_session', array(
'user_id' => $this->data()->id,
'hash' => $hash
));
} else {
$hash = $hashCheck->first()->hash;
}

Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
}

return true;
}
}
}

return false;
}

最佳答案

您可以使用此查询从您的 users 表中获取 row_id

$sql = "select * from `users` where username = $input OR email = $input";

关于php - 使用用户名和/或电子邮件登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29183223/

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