MySQL,为列生成随机的42个字符的字符串

标签 mysql database string random

我需要为表中每一行的列生成一个 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/

相关文章:

Mysql:使用正则表达式匹配作为字段?

mysql - 重新排序/重置自动增量主键

python - 提取名称和版本号,并避免不匹配?

java - 计算 JTable 的最宽列

mysql - 添加两个表中的多个(小数)值以更新第三个表

database - 为 nhibernate 指定命名的 SQL Server 实例

Python 'str' 对象在使用函数变量调用类实例时没有属性 'name'

c# - 使用 contains 进行不区分大小写的字符串搜索

php - 显示具有多个值的唯一列名称

mysql - Node mysql2无法访问更新结果