版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1333783
都是印象中匹配ASCII码进行随机读取的实现方法,此处可提供一个方法作为参考:
function get_rand_ascii($pw_length = 5){
$randpwd = '';
for ($i = 0; $i < $pw_length; $i++) {
$randpwd .= chr(mt_rand(33, 99));
}
return $randpwd;
}
function getRandomString($length=5) {
//可任意添加需要的字符
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
$charsLen = count ( $chars ) - 1;
shuffle ( $chars ); // 将数组打乱
$output = "";
for($i = 0; $i < $length; $i ++) {
$output .= $chars [mt_rand ( 0, $charsLen )];
}
return $output;
}
function get_pass( $length = 8 ) {
// 密码字符集,可任意添加你需要的字符
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
$password = '';
for ( $i = 0; $i < $length; $i++ ) {
// 这里提供两种字符获取方式
// 第一种是使用 substr 截取$chars中的任意一位字符;
// 第二种是取字符数组 $chars 的任意元素
// $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
return $password;
}
发现的新思路,可使用加密算法进行获取
public function getRand(){
$rand = rand(10000,100000);
$str = substr(base64_encode($rand), 0, 5);
return $str;
}
$password = 'password1232456';//前端 获取的原始密码
//数据库存放的 使用BCRYPT算法加密的密码
//此处仅为测试,实际应用时应从数据表中查询获得
//$db_pass = '$2y$10$2vJJC.rb/swAUnTfc9B94.l/ix75kiZHvOZFpu0Dd8uzp07YWlj4q';
$db_pass = password_hash($password, PASSWORD_BCRYPT);
if (password_verify($password , $db_pass)){
echo "密码匹配";
}else{
echo "密码错误";
}