IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

CI框架里用的验证码

废墟 2011-09-19 23:42:00 累计浏览 5,821 次
本机暂存
自带那个不太喜欢,于是另外折腾了个
<?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->imgmt_rand(200255), mt_rand(200255), mt_rand(200255
));
        
//画出一个矩形
        
imagefilledrectangle($this->img0$this->height$this->width0$color
);
    }

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

    
//随机线条
    
private function createLine
() {
        
//随机线条
        
for ($i 0$i 6$i
++) {
            
$color imagecolorallocate($this->imgmt_rand(0156), mt_rand(0156), mt_rand(0156
));
            
imageline($this->imgmt_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. 等了十年的 Go 链式管道,终于来了:seq 让你像写 Scala 一样写 Go (2026-06-25 18:38:18)
  2. Go 实验特性详解 (2026-06-21 10:05:27)
  3. amd64 微架构级别对 Go 程序性能提升多少? (2026-06-21 09:38:49)

查看更多 后端 文章 →

建议继续学习

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