Home  >  Article  >  Backend Development  >  PHP生成4位可能5位的字母和数字组合验证码

PHP生成4位可能5位的字母和数字组合验证码

WBOY
WBOYOriginal
2016-06-13 12:23:581099browse

PHP生成4位或者5位的字母和数字组合验证码

<?php $img = imagecreatetruecolor(100, 40);//创建一个100*40的画布$black = imagecolorallocate($img, 0x00, 0x00, 0x00);//定义黑色$green = imagecolorallocate($img, 0x00, 0xFF, 0x00);//定义绿色$white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);//定义白色imagefill($img,0,0,$white);	//使用白色填充画布//生成数字和字母的4位或者5位的随机验证码$str = &#39;abcdefghijkmnpqrstuvwxyz1234567890&#39;;$arr = str_split($str);shuffle($arr);$code = &#39;&#39;;$arr = implode($arr);$code = substr($arr,rand(0,5),rand(4,5));imagestring($img, 5, 20, 15, $code, $black);//将验证码绘制到画布上//加入噪点干扰for($i=0;$i<50;$i++) {  imagesetpixel($img, rand(0, 100) , rand(0, 100) , $black);   imagesetpixel($img, rand(0, 100) , rand(0, 100) , $green);}//加入图像干扰for($i=0;$i<20;$i++){	imageline($img,rand(0, 100), rand(0, 100), rand(0, 100),rand(0, 100), $green);}//输出验证码header("content-type: image/png");imagepng($img);imagedestroy($img);

版权声明:本文为博主原创文章,未经博主允许不得转载。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn