34

使用 PHP,有哪些方法可以生成可以存储在数据库中并用于电子邮件确认的随机确认码?我一生都想不出一种方法来生成可以从用户个人资料中生成的唯一编号。这样我就可以使用一个函数来使数字足够小以包含在 URL 中(请参阅此链接)。请记住,用户必须单击链接以“确认/激活”他/她的帐户。如果我不能使用数字,我可以同时使用字母和数字。

话虽如此,我尝试将用户名与“盐”一起散列以生成随机代码。我知道必须有更好的方法,所以让我们听听。

4

5 回答 5

57
$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));
于 2010-01-18T20:30:54.057 回答
10

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
于 2010-01-18T20:45:37.267 回答
4

接受的答案建议使用 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 文档的代码:

$random = openssl_random_pseudo_bytes(32, $isSourceStrong);
if (false === $isSourceStrong || false === $random) {
    throw new \RuntimeException('IV generation failed');
}

然后像以前一样使用生成的随机值:

$random_hash = bin2hex($random)
于 2014-08-30T01:51:04.737 回答
3

决定我需要一些更强大和增加功能的东西。所以这就是我想出的。

/**
 * 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;

 } 
于 2013-03-04T02:15:28.270 回答
-1
  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

我认为很难重复这个数字,因为它永远不会是同一天,也不会是同一小时,也不会是相同的毫秒时间。

于 2017-02-16T15:17:36.770 回答