Posted in PHP onApril 01, 2021
<?php
/*
* 方法类
* */
class functions
{
/**
* PHP随机生成验证码函数
*
* @param array
* @return mixed
*/
function randCode($params = [])
{
$num = $params['num'] ?? 4; //验证码个数
$isLetter = $params['isLetter'] ? $params['isLetter'] : 1; //1是纯数字 2是字母和数字的组合
if($isLetter==1){
for($i=1;$i<=$num;$i++){
$codeMin.=0;
$codeMax.=9;
}
return rand($codeMin,$codeMax);
}
if($isLetter==2){
//如果想调整权重,自己可以根据需求修改$codeArr这个一位数组
$codeArr = ['1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','e','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0'];
$codeKeys = array_rand($codeArr,$num);
shuffle($codeKeys);
foreach ($codeKeys as $codeValue){
$codeStr .= $codeArr[$codeValue];
}
return $codeStr;
}
}
}
//测试生成验证码方法
$re = (new functions())->randCode([
'num' => 6, //需要的个数
'isLetter' => 2, //1是纯数字 2是数字加字符串
]);
print_r($re);
php随机生成验证码,php随机生成数字,php随机生成数字加字母!
- Author -
JESON日志声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@