header('content-type:text/html;charset=utf-8'); /** * 默认产生4位数字验证码 * 1 数字 2 字母 3 数字+字母 4 汉字 * @param number $type * @param number $length * @return string */ echo getVerify(); echo '<br/>'; echo getVerify(2); echo '<br/>'; echo getVerify(3, 5); echo '<br/>'; echo getVerify(4, 2); function getVerify($type = 1, $length = 4) { $str1 = join('', range(0, 9)); $str2 = join('', range('a', 'z')); $str3 = join('', range('A', 'Z')); if ($type == 1) { $string = $str1; } elseif ($type == 2) { $string = $str2 . $str3; } elseif ($type == 3) { $string = $str1 . $str2 . $str3; } elseif ($type == 4) { $str = '水电费经理为家人离开家就是打了快放假了山东快放假了快速的分解'; $arr = str_split($str, 3); for ($i = 1; $i <= $length; $i++) {
session_start(); //将随机数存入session中 $_SESSION['authnum'] = ""; $im = imagecreate(50, 20); //制定图片背景大小 $black = ImageColorAllocate($im, 0, 0, 0); //设定三种颜色 $white = ImageColorAllocate($im, 255, 255, 255); $gray = ImageColorAllocate($im, 200, 200, 200); imagefill($im, 0, 0, $gray); //采用区域填充法,设定(0,0) while (($authnum = rand() % 100000) < 10000) { } //将四位整数验证码绘入图片 //$_SESSION['authnum']=$authnum; $strings = getVerify(); // print("\n\t"); // print_r($strings); // print("\n\t"); // echo "$strings"; // print("\n\t"); $_SESSION['authnum'] = str_ireplace(" ", "", $strings); imagestring($im, 5, 1, 3, $strings, $black); // 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 座标处(图像的左上角为 0, 0)。 //如果 font 是 1,2,3,4 或 5,则使用内置字体 for ($i = 0; $i < 50; $i++) { $randcolor = ImageColorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($im, rand() % 70, rand() % 30, $randcolor); } //$strings = getVerify(); //$_SESSION['aaaa']=$strings;