PHP图形验证码的具体实现方法
我们现在在网站注册等地方经常会见到需要你输入验证码的请款,它就是将随机数字或符号以图片的形式展现在用户面前,并提供验证后才能使用相关功能。今天我们就介绍
具体实现代码如下:
- php
- class rndnum{
- function rnd(){
- srand((double)microtime()*1000000);
- $rnd_number=array(
- 1=>1,
- 2=>2,
- 3=>3,
- 4=>4,
- 5=>5,
- 6=>6,
- 7=>7,
- 8=>8,
- 9=>9,
- 10=>a,
- 11=>b,
- 12=>c,
- 13=>d,
- 14=>e,
- 15=>f,
- 16=>g,
- 17=>h,
- 18=>i,
- 19=>j,
- 20=>k,
- 21=>l,
- 22=>m,
- 23=>n,
- 24=>o,
- 25=>p,
- 26=>q,
- 27=>r,
- 28=>s,
- 29=>t,
- 30=>u,
- 31=>v,
- 32=>w,
- 33=>x,
- 34=>y,
- 35=>z,
- 36=>0
- );
- $result=array_rand($rnd_number,6);
- $j=count($result);
- for ($i=0;$i<$j;$i++) {
- $re.=$rnd_number[$result[$i]];
- }
- //$re=$rnd_number[$result[1]].$rnd_number[$result[2]].$rnd_number[$result[3]].$rnd_number[$result[4]].$rnd_number[$result[5]].$rnd_number[$result[6]].$rnd_number[$result[7]];
- //return array_keys($result);
- return $re;
- }
- }
- /*用法
- $rndnum=new rndnum();
- $num=$rndnum->rnd();
- echo $num;
- */
- ?>
生成PHP图形验证码的图片,将随机数填充到里边:
- php
- Header(Content-type: image/PNG);
- require_once(rndnum.php);
- $rndnum=new rndnum();
- $authnum=$rndnum->rnd();
- session_start();
- $_SESSION[extrra_code]=$authnum;
- $im = imagecreate(72,20);
- $black = ImageColorAllocate($im, 0,0,0);
- $white = ImageColorAllocate($im, 255,255,255);
- $gray = ImageColorAllocate($im, 200,200,200);
- imagefill($im,0,0,$gray);
- imagestring($im,5,10,3,$authnum,$black);
- for($i=0;$i<200;$i++) //加入干扰象素
- {
- $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
- imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
- }
- ImagePNG($im);
- ImageDestroy($im);
- ?>
以上就是全部PHP图形验证码的实现代码,希望对大家有所帮助。
本文地址:http://www.phprm.com/frame/php1003546.html
转载随意,但请附上文章地址:-)