我正在使用RedBean PHP尝试注册/获取用户。也就是说,给定一个逗号分隔的列表,我需要
$emails = explode(',', trim($app->request->post('emails')));然后将每封电子邮件传递给这个函数。
function registerOrGetUser($email)
{
echo("R or G " . $email . "<br>");
$user = R::findOne('user', ' email = ? ', array($email));
if(!$user)
{
echo("Couldn't find user " . $email . ", creating new user.<br>");
//user does not exist, register them
$user = R::dispense('user');
$password = $random = substr(md5(rand()),0,8);
$user->email = $email;
$user->password = md5($password);
$user->role = 0;
R::store($user);
mail($user->email, "Welcome to imgstat!", "Welcome to imgstat, " . $user->email . "! An account has been created for you. Please sign in with this email address, and the following password: " . $password);
}
return $user;
}注意用于调试的echo语句。我遇到的问题是,它有时只能检测用户是否已经存在。也就是说,如果我试图添加
test@test.test它获取用户(而不注册新用户)。但是,如果我尝试
test@test.test, test@test.test它在数据库中创建了第二个用户--据我所知,有完全相同的电子邮件!从那时起,我可以添加我想要的多少test@test.tests -他们将永远得到用户,而不是创建重复。但出于某种原因,如果电子邮件不是逗号分隔列表中唯一的条目,则始终会创建第一个副本。
有什么想法吗?
发布于 2014-01-18 02:24:14
小心空白处。您的示例test@test.test, test@test.test, test@test.test完全符合您描述的问题。如果你用“”来爆炸那根绳子,你就会得到:
Array
(
[0] => test@test.test
[1] => test@test.test
[2] => test@test.test
)这就是为什么在创建具有邮件空间的第二个用户之后,成功并从那时起被正确地找到。若要防止空白,只需使用registerOrGetUser(trim($email))调用函数即可。
发布于 2014-01-18 02:24:17
找到了!
愚蠢的错误-使用trim只从列表的开头/结尾删除空格,所以有“test@test.com”和“test@test.com”.
我的解决办法:
$emails = explode(',', str_replace(' ', '', $app->request->post('emails')) );https://stackoverflow.com/questions/21199139
复制相似问题