当前位置: 代码迷 >> PHP >> PHP自动生成随绝密码
  详细解决方案

PHP自动生成随绝密码

热度:87   发布时间:2016-04-28 22:13:51.0
PHP自动生成随机密码
<?phpfunction rand_string($len = 16, $keyword = '') {    if (strlen($keyword) > $len) {//关键字不能比总长度长        return false;    }    $str = '';    $chars = 'abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHIJKMNPQRSTUVWXYZ'; //去掉1跟字母l防混淆                if ($len > strlen($chars)) {//位数过长重复字符串一定次数        $chars = str_repeat($chars, ceil($len / strlen($chars)));    }    $chars = str_shuffle($chars); //打乱字符串    $str = substr($chars, 0, $len);    if (!empty($keyword)) {        $start = $len - strlen($keyword);        $str = substr_replace($str, $keyword, mt_rand(0, $start), strlen($keyword)); //从随机位置插入关键字    }    return $str;}echo rand_string(16,"ab"); //output example:V8bNY6SmkeywordB?>

?

  相关解决方案