获取随机数作为验证码

/**
* 获取随机数作为验证码
* @param int $digit 验证码位数
* @param int $type 验证码类型 1:默认数字 2:字符 3:数字+字符
* @return string $authnum  返回验证码
*/
function randomStr($digit = 6, $type = 1)
{
  if ($type == 1) {
     $char = "0,1,2,3,4,5,6,7,8,9";
     $x = 9;
  } elseif ($type == 2) {
     $char = "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";
     $x = 25;
  } else {
     $char = "0,1,2,3,4,5,6,7,8,9,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";
     $x = 35;
  }

  $authnum = "";
  $list = explode(",", $char);
  for ($i = 0; $i < $digit; $i++) {
     $randnum = rand(0, $x); // 10+26;
     $authnum .= $list[$randnum];
  }
  return $authnum;
}


Jsky博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论