+ -
当前位置:首页 → 问答吧 → PHP GD图片验证码类

PHP GD图片验证码类

时间:2008-05-15

来源:互联网

昨天拿一个小时研究了一下PHP GD类库的使用方法,实际就是看了一本书中的PHP图像技术一章节,感觉PHP利用GD类库操作图片的确很方便。晚上回家忽然萌生了写一个PHP图片验证码类的想法。所以就有了下面的代码,呵呵,现在贴出来,请大家多提宝贵意见,谢谢!
复制PHP内容到剪贴板
PHP代码:

<?php
/*
@date           20080514
@author        hluan
                      [url=http://watir.cn]http://watir.cn[/url]
@Package    Identifying.Code.Pic.Class
@attention    Before using this class,you should use session_start();
*/
//session_start();
class icode
{
function __construct(){
  header('Content-Type:image/png');
}
function __destruct(){
  imagedestroy($png);
}
/**************************************************
  @len     The length of the identifying code.
  @type     1:only number identifying code;
  *              2:only letter identifying code;
  *              0:number and letter identifying code.
  @attention   We store identifying code in $_SESSION['icode'].
**************************************************/
function getcode($len,$type){
  switch ($type){
   case 1;
    $str = "1234567890";
    break;
   case 2;
    $str = "abcdefghijklmnopqrstuvwxyz";
    break;
   case 0;
    $str = "1234567890abcdefghijklmnopqrstuvwxyz";
    break;
  }
  $result = "";
  $length = strlen($str) - 1;
  $num = 0;
  for ($i=0;$i<$len;$i++){
   $num = rand(0,$length);
   $a = $str[$num];
   $result = $result.$a;
  }
  /*Before using it,you should use session_start()*/
  $_SESSION['icode'] = $result;//Store in session.
  //echo "session:".$_SESSION['icode'];
  //die();
  $png = imagecreate(60,30);
  $white = imagecolorallocate($png,255,255,255);//background
  $red = imagecolorallocate($png,255,0,0);
  $blue = imagecolorallocate($png,0,0,255);
  $brown = imagecolorallocate($png,100,0,0);
  $black = imagecolorallocate($png,0,0,0);//Identifying Code
  imagefill($png,0,0,$white);
  $k = rand(0,3);
  if (0 == $k){
   for ($n=0;$n<60;$n++){
    $y = 15*sin($n/30*pi());
    imagesetpixel($png,$n,15+$y,$red);
   }
  }else if (1 == $k){
   for ($n=0;$n<60;$n++){
    $y = 15*cos($n/60*pi());
    imagesetpixel($png,$n,15+$y,$red);
   }
  }else if (2 == $k){
   for ($n=0;$n<60;$n++){
    $y = 15*sin($n/45*pi());
    imagesetpixel($png,$n,15+$y,$brown);
   }
  }else if (3 == $k){
   for ($n=0;$n<60;$n++){
    $y = 15*cos($n/30*pi());
    imagesetpixel($png,$n,15+$y,$brown);
   }
  }
  for ($k=0;$k<10;$k++){
   $i = rand(3,60);//width
   $j = rand(3,15);//height
   imageline($png,$i-3,$j-3,$i,$j,$black);
  }
  imagestring($png,5,3,5,$result,$blue);
  $icode = imagepng($png);
  return $icode;
}
}
/*测试代码
$icode = new icode();
$icp = $icode->getcode(6,0);
echo $icp;
*/

[ 本帖最后由 littlehehe 于 2008-5-15 14:29 编辑 ]

作者: littlehehe   发布时间: 2008-05-15

有广告嫌疑?

作者: PHPChina   发布时间: 2008-05-15

引用:
原帖由 luzhou 于 2008-5-15 12:42 发表
有广告嫌疑?
太敏感了吧... ...好吧,我把图片删除了... ...

作者: luzhou   发布时间: 2008-05-15

这个如何在表单使用啊
怎么获取要验证的字符啊

作者: littlehehe   发布时间: 2008-05-31

不是吧... ...
好吧,简单写一下... ...
首先确保session_start();
(以下以smarty为例)
mod里写:
复制PHP内容到剪贴板
PHP代码:
$geticode = new icode();
$icode = $geticode->getcode(6,0);
$smarty->assign('icode',$icode);

模板form里写:
复制PHP内容到剪贴板
PHP代码:
验证码:{$icode}
<input type="text" name="icode" size=20/>

处理程序里写:
复制PHP内容到剪贴板
PHP代码:
$icode = $_POST['icode'];
if ($icode == $_SESSION['icode']){
//验证成功
}

作者: sanler   发布时间: 2008-06-03

辛苦啦!

作者: littlehehe   发布时间: 2008-06-04

学习了~~~  谢谢

作者: phpcaicai   发布时间: 2008-06-05

怎么看起来很眼熟....

作者: liynid   发布时间: 2008-06-05

千篇一律。。。。

作者: CrossMaya   发布时间: 2008-06-09

引用:
原帖由 kv163 于 2008-6-9 09:45 发表
千篇一律。。。。
呵呵,那么你能给我个你写的不千篇一律的么?
写php到现在,从基本html嵌套开发到MVC模式开发,最后现在用熟练了symfony这类框架,我感觉不能说完全千篇一律,但是基本实现的功能差不多都是一个套路。
就好比当兵拿枪,从老式手拉步枪到56半自动步枪,从81杠自动步枪到95步枪,三点一线射击原理永久不变,您能说这个也是千篇一律么?
请赐教,谢谢!

作者: kv163   发布时间: 2008-06-10

热门下载

更多