当前位置:Gxlcms > PHP教程 > PHP随机生成不重复的8位卡号(数字)和卡密(字符串)

PHP随机生成不重复的8位卡号(数字)和卡密(字符串)

时间:2021-07-01 10:21:17 帮助过:48人阅读

一、生成不重复的随机数字,可自定义长度

  1. /**
  2. * 生成不重复的随机数字
  3. * @param int $start 需要生成的数字开始范围
  4. * @param int $end 结束范围
  5. * @param int $length 需要生成的随机数个数
  6. * @return number 生成的随机数
  7. */
  8. function getRandNumber($start=0,$end=9,$length=8){
  9. //初始化变量为0
  10. $connt = 0;
  11. //建一个新数组
  12. $temp = array();
  13. while($connt < $length){
  14. //在一定范围内随机生成一个数放入数组中
  15. $temp[] = mt_rand($start, $end);
  16. //$data = array_unique($temp);
  17. //去除数组中的重复值用了“翻翻法”,就是用array_flip()把数组的key和value交换两次。这种做法比用 array_unique() 快得多。
  18. $data = array_flip(array_flip($temp));
  19. //将数组的数量存入变量count中
  20. $connt = count($data);
  21. }
  22. //为数组赋予新的键名
  23. shuffle($data);
  24. //数组转字符串
  25. $str=implode(",", $data);
  26. //替换掉逗号
  27. $number=str_replace(',', '', $str);
  28. return $number;
  29. }

二、随机生成不重复的8位卡密

  1. function makeCardPassword() {
  2. $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  3. $rand = $code[rand(0,25)]
  4. .strtoupper(dechex(date('m')))
  5. .date('d').substr(time(),-5)
  6. .substr(microtime(),2,5)
  7. .sprintf('%02d',rand(0,99));
  8. for(
  9. $a = md5( $rand, true ),
  10. $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
  11. $d = '',
  12. $f = 0;
  13. $f < 8;
  14. $g = ord( $a[ $f ] ),
  15. $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
  16. $f++
  17. );
  18. return $d;
  19. }

相关推荐:《PHP教程》

以上就是PHP随机生成不重复的8位卡号(数字)和卡密(字符串)的详细内容,更多请关注Gxlcms其它相关文章!

人气教程排行