我需要为表中每一行的列生成一个 42 个字符的字符串...它用作密码恢复字符串。它必须是 42 个字符长,如果它是唯一的将会很有帮助...
如果用户忘记密码,则生成一个字符串。在收到电子邮件后,生成一个“abc123”字符串,我认为它会执行以下操作:“从用户中选择 recovery_string =“abc123”
最佳答案
请查看此代码,我专门为这个问题生成了它。
我确信这可能不是最好的方法,但这会起作用:
编辑:变成一个函数,这样你就可以选择长度。旧代码如下。
新代码:
<?php
function GenerateRecoveryString($length)
{
$time=time();
$az="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
//$time and $az is just random stuff to generate a hash out of.
$output = md5($time.$az);//The soon to be 42 char string.
while(strlen($output)<$length)
{
$output.=md5($time.$az);//md5 only creates 32 bit strings - Lets create one that's 42 chars.
}
$output = substr($output, "0",$length);//The above example created 64 bit string - We need to cut it down.
str_shuffle($az);//Randomize the string above, to ensure a different output next time.
echo $output.' Length:'.strlen($output);
}
echo GenerateRecoveryString("42");
?>
<小时/>
<?php
$time=time();
$az="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
//$time and $az is just random stuff to generate a hash out of.
$output = md5($time.$az);//The soon to be 42 char string.
while(strlen($output)<"42")
{
$output.=md5($time.$az);//md5 only creates 32 bit strings - Lets create one that's 42 chars.
}
$output = substr($output, "0","42");//The above example created 64 bit string - We need to cut it down.
str_shuffle($az);//Randomize the string above, to ensure a different output next time.
echo $output.' Length:'.strlen($output);
?>
关于MySQL,为列生成随机的42个字符的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19017337/