您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在PHP中生成随机密码

在PHP中生成随机密码

rand()不是加密安全的伪随机生成器。寻找其他地方以在PHP生成加密安全的伪随机字符串。

试试这个(使用strlen代替count,因为count在字符串上总是1):

function randomPassword() {
    $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 8; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}
php 2022/1/1 18:15:34 有346人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶