CI框架里用的验证码

废墟 2011-09-19 23:42:00 累计浏览 6,051 次
自带那个不太喜欢,于是另外折腾了个
<?php
if (!defined('BASEPATH'
))
    exit(
'No direct script access allowed'
);

class 
vcode extends CI_Model 
{

//    private $charset = "abcdefghizklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";   //随机因子  
    
private $charset = "1234567890";   
//随机因子  
    
private $code;  
//验证码文字
    
private $codelen = 4; 
//验证码显示几个文字
    
private $width = 75;   
//验证码宽度
    
private $height = 25;   
//验证码高度
    
private $img;    
//验证码资源句柄
    
private $font;  
//指定的字体
    
private $fontsize = 16;  
//指定的字体大小
    
private $fontcolor;  
//字体颜色  随机

    //构造类  编写字体

    
public function __construct
() {
        
parent::__construct
();
        
$this->font = APPPATH . 'font/LiberationSans-Bold.ttf'
;
    }

    
//创建4个随机码
    
private function createCode
() {
        
$_leng = strlen($this->charset) - 1
;
        for (
$i = 0; $i < $this->codelen; $i
++) {
            
$this->code.=$this->charset[mt_rand(0, $_leng
)];
        }
        return 
$this->code
;
    }

    
//创建背景
    
private function createBg
() {
        
//创建画布 给一个资源jubing
        
$this->img = imagecreatetruecolor($this->width, $this->height
);
        
//背景颜色
        
$color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255
));
        
//画出一个矩形
        
imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color
);
    }

    
//创建字体
    
private function createFont
() {
        
$_x = ($this->width / $this->codelen);   
//字体长度
        
for ($i = 0; $i < $this->codelen; $i
++) {
            
//文字颜色
            
$color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156
));
            
//资源句柄 字体大小 倾斜度 字体长度  字体高度  字体颜色  字体  具体文本
            
imagettftext($this->img, $this->fontsize, mt_rand(-18, 18), $_x * $i + mt_rand(1, 5), $this->height/1.2, $color, $this->font, $this->code[$i
]);
        }
    }

    
//随机线条
    
private function createLine
() {
        
//随机线条
        
for ($i = 0; $i < 6; $i
++) {
            
$color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156
));
            
imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color
);
        }
//        //随机雪花
//        for ($i = 0; $i < 45; $i++) {
//            $color = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));
//            imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '.', $color);
//        }
    
}

    
//输出背景
    
private function outPut
() {
        
//生成标头
        
header("Expires: -1"
);
        
header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE
);
        
header("Pragma: no-cache"
);
        
header('Content-Type:image/png'
);
        
//输出图片
        
imagepng($this->img
);
        
//销毁结果集
        
imagedestroy($this->img
);
    }

    
//对外输出
    
public function doimg
() {
        
//加载背景
        
$this->createBg
();
        
//加载文件
        
$this->createCode
();
        
//加载线条
        
$this->createLine
();
        
//加载字体
        
$this->createFont
();
        
//加载背景
        
$this->outPut
();
    }

    
//获取验证码
    
public function getCode
() {
        return 
strtolower($this->code
);
    }

}


使用方法如下
<?php
public function create_vcode
() {
    
$this->load->model('vcode'
);
    echo 
$this->vcode->doimg
();
    
$cookie 
= array(
        
'name' => 'vcode'
,
        
'value' => $this->common->authCode($this->vcode->getCode(), 'ENCODE', _KEY
),
        
'expire' => '1800'
,
    );
    
$this->input->set_cookie($cookie
);
    die;
}


字体文件在此

同分类推荐文章

  1. NGINX 1.31.7: Native GeoIP2 support, HTTP/2 priorities, and QUIC improvements (2026-11-13 23:40:02)
  2. 说说广场 API 0.31.0 版的调用代码说明 (2026-11-13 19:10:02)
  3. NGINX Routing on HTTP Request Body (2026-10-27 14:10:42)

查看更多 后端 文章 →

建议继续学习

  1. 使用gettext来支持PHP的多语言 (累计阅读 39,702)
  2. WordPress插件开发 -- 在插件使用数据库存储数据 (累计阅读 29,569)
  3. Paypal接口详细代码(PHP版,非API接口) (累计阅读 19,821)
  4. 我的PHP,Python和Ruby之路 (累计阅读 13,719)
  5. include(“./file.php”)和include(“file.php”)区别 (累计阅读 13,274)
  6. 15个最好的免费开源电子商务平台 (累计阅读 12,950)
  7. Redis消息队列的若干实现方式 (累计阅读 12,536)
  8. 到底什么是MVC? (累计阅读 12,385)
  9. 整理了一份招PHP高级工程师的面试题 (累计阅读 12,222)
  10. Rolling cURL: PHP并发最佳实践 (累计阅读 11,972)