php 生成 验证码的例子

  1. /** 
  2.   +---------------------------------------------------------- 
  3.  * 生成随机字符串  CuPlayer.com 酷播
  4.   +---------------------------------------------------------- 
  5.  * @param int       $length  要生成的随机字符串长度 
  6.  * @param string    $type    随机码类型:0,数字+大小写字母;1,数字;2,小写字母;3,大写字母;4,特殊字符;-1,数字+大小写字母+特殊字符 
  7.   +---------------------------------------------------------- 
  8.  * @return string 
  9.   +---------------------------------------------------------- 
  10.  */ 
  11. function randCode($length = 5, $type = 0) { 
  12.     $arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "~@#$%^&*(){}[]|"); 
  13.     if ($type == 0) { 
  14.         array_pop($arr); 
  15.         $string = implode("", $arr); 
  16.     } elseif ($type == "-1") { 
  17.         $string = implode("", $arr); 
  18.     } else { 
  19.         $string = $arr[$type]; 
  20.     } 
  21.     $count = strlen($string) - 1; 
  22.     $code = ''; 
  23.     for ($i = 0; $i < $length; $i++) { 
  24.         $code .= $string[rand(0, $count)]; 
  25.     } 
  26.     return $code; 
  27.  
  28. echo randCode(6,1); 
原文地址:https://www.cnblogs.com/lijiageng/p/5800830.html