昨天利用Kcaptcha在CakePHP中实现了验 证码功能,但发现Kcaptcha验证码特大了,虽然可以调整大小,但会经常出现看不到的情况,而且在IE下很不清晰,在目前项目上用肯定会被KO掉。看 了一下它在CakePHP的调用过程,其实就是向CakePHP添加了一个组件,于是我就想把自己的验证码程序也添加进去。SO 开始:

1、第一步和Kcaptcha有很大区别,因为不用下载。在/app/vendors目录下建立一个vcode目录

2、在vcode目录下建立vcode.php文件,内容为你生成验证码的程序段,当然可能需要修改一下,反正我这里是按照Kcaptcha的方式写的。内容如下:

PHP代码
  1. class VCODE{   
  2.     function VCODE(){   
  3.         ob_start();   
  4.         $width=65;   
  5.         $height=20;   
  6.         $sourcestrings="0123456789aqwertyuiopasdfghjklzxcvbnm
  7. QWERTYUIOPASDFGHJKLZXCVBNM";   
  8.         $image=imagecreate($width,$height);   
  9.         $colorarrs=array(   
  10.             imagecolorallocate($image,255,255,255),//white   
  11.             imagecolorallocate($image,0  ,0  ,  0)//black   
  12.         );   
  13.         unset($this->sessionval);   
  14.         imagesetthickness($image,3);   
  15.         //随机得到字符串个数   
  16.         $strsize=rand(3,5);   
  17.         imagefill($image,0,0,$colorarrs[0]);   
  18.         //一个个的写字符串到图片   
  19.         for($i=0;$i<$strsize;$i++){   
  20.             $i_temp=rand(1,62);   
  21.             $this->sessionval .=$sourcestrings[$i_temp];   
  22.             $fontcolor=imagecolorallocate($image,rand(0,155),rand(0,155),rand(0,155));   
  23.             $y_i = $height/2 + $font_size /3 ;   
  24.             imagechar($image,5, 1+ $i * $width /$strsize,5,
  25. $sourcestrings[$i_temp],$fontcolor);   
  26.         }   
  27.         //写入session,以后验证用   
  28.         //unset($_SESSION['vCode']);   
  29.         //$_SESSION['vCode'] = $sessionval;   
  30.         //添加杂点   
  31.         for($i=0;$i<$width /$height *2;$i++)   
  32.         {    $i_x=rand(0,$width);   
  33.             $i_y=rand(0,$height);   
  34.             $pixelcolor=imagecolorallocate
  35. ($image,rand(0,255),rand(0,255),rand(0,255));   
  36.             imagesetpixel($image,$i_x,$i_y,$pixelcolor);   
  37.         }   
  38.   
  39.         ob_clean();   
  40.         header('content-type:image/png');   
  41.         imagepng($image);   
  42.         imagedestroy($image);   
  43.     }   
  44.     function getKeyString(){   
  45.         return $this->sessionval;   
  46.     }   
  47. }  

3、在/app/controllers/components目录下建立Vcode.php文件,内容如下:

PHP代码
  1. class VcodeComponent extends Object{   
  2.     function startup($controller){   
  3.         $this->controller=$controller;   
  4.     }   
  5.   
  6.     function render(){   
  7.         App::import('Vendor','vcode/vcode');   
  8.         $vcode=new VCODE();   
  9.         $this->controller->Session->write('vcode',$vcode->getKeyString());   
  10.     }   
  11. }  

4、在你想在调用的controllers中加入,如indexController

PHP代码
  1. var $components=array('Session','Vcode');  

5、再在这个controllers中加入一个调用方法:

PHP代码
  1. function vcode(){   
  2.     $this->Vcode->render();   
  3. }  

好了,准备工作就完成了,下面就是在view里用了。

6、在你要调用的view里写上:

XML/HTML代码
  1. <img src="<?php echo $html->url('/index/vcode');?>" style="cursor:pointer;" onclick="document.getElementById('vcode').
  2. src='<?php echo $html->url('/index/vcode');?>?'+Math.random();" id="vcode" />  

刷新一下页面看看,OK了!!