[英]Generating confirmation code for an email confirmation
使用PHP,有什么方法可以生成可以存储在数据库中并用于电子邮件确认的随机确认代码? 我一辈子都想不出一种可以从用户个人资料中生成唯一编号的方法。 这样,我可以使用一个函数来使数字小到足以包含在URL中( 请参阅此链接 )。 请记住,用户必须单击链接以“确认/激活”他/她的帐户。 如果我不能使用数字,那么使用字母和数字都不会有问题。
话虽如此,我已经尝试过对用户名和“盐”进行哈希运算以生成随机代码。 我知道必须有更好的方法,所以让我们听听吧。
$random_hash = md5(uniqid(rand(), true));
这将是32个字母数字字符,且唯一。 如果您希望它更短些,请使用substr():
$random_hash = substr(md5(uniqid(rand(), true)), 16, 16); // 16 characters long
生成随机数据的替代方法包括:
$random_hash = md5(openssl_random_pseudo_bytes(32));
$random_hash = md5(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
// New in PHP7
$random_hash = bin2hex(random_bytes(32));
1)在数据库中创建一个激活字段
2)注册后发送电子邮件
3)创建一个要包含在电子邮件中的链接,使用唯一标识符看起来像这样
欢迎用户名感谢您的注册。
请点击下面的链接激活您的帐户
domain.com/register.php?uid=100&activate=1
4)将激活字段更新为true
(来源: jackborn.com )
$email_encrypt = urlencode($email);
$special_string = 'maybeyourcompanynamereversed?';
$hash = md5($email_encrypt.$special_string);
Here is the link that is sent to the email that was provided:
http://yourdoman.com/confirm.php?hash='.$hash.'
The actual link will look something like this:
http://yourdomain.com/confirm.php?hash=00413297cc003c03d0f1ffe1cc8445f8
可接受的答案建议使用PHP的uniqid()
的哈希值。 uniqid的文档明确警告它不会创建“随机或不可预测的字符串”,并着重指出“ 此功能不得用于安全目的。 ”
如果担心可能会猜到确认码(这就是发出验证码的全部内容),则您可能希望使用更加随机的生成器,例如openssl_random_pseudo_bytes()
。 然后,您可以使用bin2hex()
将其转换为漂亮的字母数字。 以下内容看起来像约翰·孔德的答案的输出,但(据说)更加随机且难以猜测:
// generate a 16 byte random hex string
$random_hash = bin2hex(openssl_random_pseudo_bytes(16))
后期附录:正如Oleg Abrazhaev指出的那样,如果要确保您的系统实际上能够在运行时生成具有加密强度的随机值, openssl_random_pseudo_bytes
接受对bool的引用来报告此情况。 来自phpinspectionsea docs的代码:
$random = openssl_random_pseudo_bytes(32, $isSourceStrong);
if (false === $isSourceStrong || false === $random) {
throw new \RuntimeException('IV generation failed');
}
然后像以前一样使用生成的随机值:
$random_hash = bin2hex($random)
决定我需要一些更强大和更强大的功能。 这就是我想出的。
/**
* Hash Gen
* @author Kyle Coots
* @version 1.0
* Allow you to create a unique hash with a maximum value of 32.
* Hash Gen uses phps substr, md5, uniqid, and rand to generate a unique
* id or hash and allow you to have some added functionality.
*
* @see subtr()
* @see md5()
* @see uniqid()
* @see rand()
*
* You can also supply a hash to be prefixed or appened
* to the hash. hash[optional] is by default appened to the hash
* unless the param prefix[optional] is set to prefix[true].
*
* @param start[optional]
* @param end[optional]
* @param hash[optional]
* @param prefix bool[optional]
*
* @return string a unique string max[32] character
*/
function hash_gen($start = null, $end = 0, $hash = FALSE, $prefix = FALSE){
// start IS set NO hash
if( isset($start, $end) && ($hash == FALSE) ){
$md_hash = substr(md5(uniqid(rand(), true)), $start, $end);
$new_hash = $md_hash;
}else //start IS set WITH hash NOT prefixing
if( isset($start, $end) && ($hash != FALSE) && ($prefix == FALSE) ){
$md_hash = substr(md5(uniqid(rand(), true)), $start, $end);
$new_hash = $md_hash.$hash;
}else //start NOT set WITH hash NOT prefixing
if( !isset($start, $end) && ($hash != FALSE) && ($prefix == FALSE) ){
$md_hash = md5(uniqid(rand(), true));
$new_hash = $md_hash.$hash;
}else //start IS set WITH hash IS prefixing
if( isset($start, $end) && ($hash != FALSE) && ($prefix == TRUE) ){
$md_hash = substr(md5(uniqid(rand(), true)), $start, $end);
$new_hash = $hash.$md_hash;
}else //start NOT set WITH hash IS prefixing
if( !isset($start, $end) && ($hash != FALSE) && ($prefix == TRUE) ){
$md_hash = md5(uniqid(rand(), true));
$new_hash = $hash.$md_hash;
}else{
$new_hash = md5(uniqid(rand(), true));
}
return $new_hash;
}
private function generateCodeSecurity()
{
list($usec, $sec) = explode(" ", microtime());
$micro = usec + $sec;
$hoy = date("Y-m-d");
$str = str_replace('-','',$hoy);
return rand($str, $micro);
}
使用此小代码,您可以生成一个随机数,范围为7到11个数字。
使用php函数:
Rand ();
Microtime ()
$hoy = date("Y-m-d");
$str = str_replace('-','',$hoy);
echo $str;
result date: 20170217
list($usec, $sec) = explode(" ", microtime());
$micro = usec + $sec;
echo $micro;
result micro varaible: 1487340849
在此函数中传递参数: rand ();
rand($str, $micro);
然后返回
例:
list($usec, $sec) = explode(" ", microtime());
$micro = usec + $sec;
$hoy = date("Y-m-d");
$str = str_replace('-','',$hoy);
$finalresult = rand($str, $micro);
echo $finalresult;
结果: 1297793555
我认为很难重复此数字,因为它永远不会是同一天,同一小时或同一毫秒时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.