在CI中,做验证类可以这样做,首先给出的是手册中的做法 
加载辅助函数 
用下面的代码加载验证码辅助函数: 
$this->load->helper('captcha'); 
可用的函数如下: 
captcha_create($data) 
根据你指定的一系列参数创建验证码图像, 返回值是一个包含此图像数据的数组. 
[array] 
( 
  'image' => IMAGE TAG 
  'time' => TIMESTAMP (毫秒) 
  'word' => CAPTCHA WORD 
) 
"image"是实际存在image标记: 
<img src="http://example.com/captcha/12345.jpg" width="140" height="50" /> 
这里的"time"是一个毫秒级的时间戳,作为图片文件名(不包含扩展名). 就像这样: 1139612155.3422 
"word"是验证码, 如果不提供, 将是一个随机字符串. 
使用验证码辅助函数: 
加载后你可以向这样产生一个验证码: 
$vals = array( 
    'word' => 'Random word', 
    'img_path' => './captcha/', 
    'img_url' => 'http://example.com/captcha/', 
    'font_path' => './path/to/fonts/texb.ttf', 
    'img_width' => '150', 
    'img_height' => 30, 
    'expiration' => 7200 
    ); 
$cap = create_captcha($vals); 
echo $cap['image']; 
验证码辅助函数必须需要GD库. 
只有 img_path 和 img_url 参数是必须的. 
如果"word"未提供, 将自动产生一个ASCII字符串. 你也可以使用自己的词库,从里面随机挑选. 
如果未提供TRUE TYPE字体的路径, 将会使用GD自带的字体. 
"captcha" 目录必须可写(666, or 777) 
"expiration" (秒) 指定了验证码图片的超时删除时间. 默认是2小时. 
配合数据库 
为了在提交表单时用到验证,你需要将create_captcha()生成的结果保存到数据库。这样,当用户提交表单时,你就可以验证数据库里是否有此验证码或是否过期。 
这是一个数据表的例子: 
CREATE TABLE captcha ( 
 captcha_id bigint(13) unsigned NOT NULL auto_increment, 
 captcha_time int(10) unsigned NOT NULL, 
 ip_address varchar(16) default '0' NOT NULL, 
 word varchar(20) NOT NULL, 
 PRIMARY KEY `captcha_id` (`captcha_id`), 
 KEY `word` (`word`) 
); 
这是一个使用数据库的例子. 一个带验证码的页面显示如下: 
$this->load->helper('captcha'); 
$vals = array( 
    'img_path' => './captcha/', 
    'img_url' => 'http://example.com/captcha/' 
    ); 
$cap = create_captcha($vals); 
$data = array( 
    'captcha_time' => $cap['time'], 
    'ip_address' => $this->input->ip_address(), 
    'word' => $cap['word'] 
    ); 
$query = $this->db->insert_string('captcha', $data); 
$this->db->query($query); 
echo '提交下面的验证码:'; 
echo $cap['image']; 
echo '<input type="text" name="captcha" value="" />'; 
然后页面提交后如下处理: 
// 首先删除旧的验证码 
$expiration = time()-7200; // 2小时限制 
$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration); 
// 然后再看是否有验证码存在: 
$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND date > ?"; 
$binds = array($_POST['captcha'], $this->input->ip_address(), $expiration); 
$query = $this->db->query($sql, $binds); 
$row = $query->row(); 
if ($row->count == 0) 
{ 
    echo "你必须提交图像上显示的验证码"; 
} 
第一种做法时,提交时,也验证验证码是否过期,比较严格, 然后是第2种做法,自己简单定义一个,比如captha.php,代码如下: 
   ?php 
session_start(); 
//登录验证码 
function GetVerify($length) 
{ 
 $strings = Array('3','4','5','6','7','a','b','c','d','e','f','h','i','j','k','m','n','p','r','s','t','u','v','w','x','y'); 
 $chrNum = ""; 
 $count = count($strings); 
 for ($i = 1; $i <= $length; $i++) { //循环随机取字符生成字符串 
 $chrNum .= $strings[rand(0,$count-1)]; 
 } 
 return $chrNum; 
} 
function code(){ 
$fontSize = 20; //定义字体大小 
$length = 5; //定义字符串长度 
$strNum = GetVerify($length); //获取一个随机字符串 
$_SESSION['verify'] = $strNum; //付值给session 
$width = 90; //定义图片宽度 
$height = 30; //定义图片高度 
$im = imagecreate($width,$height); //生成一张指定宽高的图片 
$backgroundcolor = imagecolorallocate ($im, 255, 255, 255); //生成背景色 
$frameColor = imageColorAllocate($im, 150, 150, 150); //生成边框色 
$font = './system/fonts/arial.ttf'; //提取字体文件,开始写字 
for($i = 0; $i < $length; $i++) { 
 $charY = ($height+9)/2 + rand(-1,1); //定义字符Y坐标 
 $charX = $i*15+8; //定义字符X坐标 
 //生成字符颜色 
 $text_color = imagecolorallocate($im, mt_rand(50, 200), mt_rand(50, 128), mt_rand(50, 200)); 
 $angle = rand(-20,20); //生成字符角度 
 //写入字符 
 imageTTFText($im, $fontSize, $angle, $charX,  $charY, $text_color, $font, $strNum[$i]); 
} 
for($i=0; $i <= 5; $i++) { //循环画背景线 
 $linecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); 
 $linex = mt_rand(1, $width-1); 
 $liney = mt_rand(1, $height-1); 
 imageline($im, $linex, $liney, $linex + mt_rand(0, 4) - 2, $liney + mt_rand(0, 4) - 2, $linecolor); 
} 
for($i=0; $i <= 32; $i++) { //循环画背景点,生成麻点效果 
 $pointcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); 
 imagesetpixel($im, mt_rand(1, $width-1), mt_rand(1, $height-1), $pointcolor); 
} 
imagerectangle($im, 0, 0, $width-1 , $height-1 , $frameColor); //画边框 
//ob_clean(); 
//header('Content-type: image/png'); 
imagepng($im); 
imagedestroy($im); 
} 
?> 
  然后把这个文件放在helper下,然后调用时可以这样: 
  $this->load->helper('captcha'); 
   code(); 










