像这样生成一个随机字符串,并使用用户电子邮件作为密码,这是一种选择,或者您使用任何内置的 php 函数来生成令牌。
$pool ='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$token= '';
for ($i = 0; $i < 8; $i++){
$token.= substr($pool, mt_rand(0, strlen($pool) -1), 1);
}
$cipher = new McryptCipher($user_email);
$encrypted_token= $cipher ->encrypt($token);
就验证而言,您可以选择将令牌和一些用户数据保存到一个表中,或者为令牌添加过期时间和清理功能以删除过期注册和垃圾邮件注册。
另一种选择是将临时注册保存到文件中。
$epxire_time = timestamp() + 1800; // 30 min
$pending_registration = json_encode['token' => $encrypted_token, 'user' => $user_email,'epxire' => $epire_time];
file_put_contents($path/to/file/,$pending_registrations);
向用户发送一封电子邮件确认,其中包含验证链接。
验证过程,一般会从url中获取参数;
$token = $_GET['token'];
// additional param
// decrypt the token
$token = $cipher->decrypt($token );
$file = file_get_contents($path/to/file);
然后像你通常做的那样验证令牌、验证令牌过期、user_email……
if($valid_token){
// save user, redirect to login
}else{
// return response invalid token
return json_encode['statusCode' => 400, 'errorMessage' => 'Invalid ...','urltorequestnewotken' => 'http://...'];
}
将挂起的注册保存到临时文件中,这只是另一种方式,可以最大限度地减少垃圾邮件和机器人对您的数据库造成的影响,就像您使用验证码一样。
希望对您有所帮助。