CI框架中,扩展验证码类。
使用CI框架的朋友,应该都知道CI框架的的验证码辅助函数,不太好用。它需要写入到数据库中,然后再进行比对。
大家在实际项目中,好像不会这样去使用,因为会对数据库造成一定的压力。
所以,我们还是利用session来临时存储验证码,比较的稳妥。
下面附上验证码类的代码。这个类是放在libraries这个库文件夹下。
<?php /**
* 验证码类
*/ class Code{
//资源
private $img;
//画布宽度
public $width = 150;
//画布高度
public $height = 45;
//背景颜色
public $bgColor = "#ffffff";
//验证码
public $code;
//验证码的随机种子
public $codeStr = "123456789abcdefghijklmnpqrstuvwsyz";
//验证码长度
public $codeLen = 4;
//验证码字体
public $font = "";//具体环境具体需要更改路径
//验证码字体大小
public $fontSize = 22;
//验证码字体颜色
public $fontColor = ""; /**
* 构造函数
*/
public function __construct($arr = array()) { $width = '';
$height = '';
$codeLen = '';
$fontSize = '';
$bgColor = '';
$fontColor = ''; if(!empty($arr)){
extract($arr);
}
$this->font = BASEPATH . "fonts/font.ttf";
if (!is_file($this->font)) {
error("验证码字体文件不存在");
}
$this->width = empty($width) ? $this->width : $width;
$this->height = empty($height) ? $this->height : $height;
$this->bgColor = empty($bgColor) ? $this->bgColor : $bgColor;
$this->codeLen = empty($codeLen) ? $this->codeLen : $codeLen;
$this->fontSize = empty($fontSize) ? $this->fontSize : $fontSize;
$this->fontColor = empty($fontColor) ? $this->fontColor : $fontColor;
$this->create();//生成验证码
} /**
* 生成验证码
*/
private function createCode() {
$code = '';
for ($i = 0; $i < $this->codeLen; $i++) {
$code .= $this->codeStr [mt_rand(0, strlen($this->codeStr) - 1)];
}
$this->code = strtoupper($code);
if(!isset($_SESSION)){
session_start();
}
$_SESSION ['code'] = $this->code;
} /**
* 返回验证码
*/
public function getCode() {
return $this->code;
} /**
* 建画布
*/
public function create() {
if (!$this->checkGD())
return false;
$w = $this->width;
$h = $this->height;
$bgColor = $this->bgColor;
$img = imagecreatetruecolor($w, $h);
$bgColor = imagecolorallocate($img, hexdec(substr($bgColor, 1, 2)), hexdec(substr($bgColor, 3, 2)), hexdec(substr($bgColor, 5, 2)));
imagefill($img, 0, 0, $bgColor);
$this->img = $img;
$this->createLine();
$this->createFont();
$this->createPix();
$this->createRec();
}
/**
* 画线
*/
private function createLine(){
$w = $this->width;
$h = $this->height;
$line_height = $h/10;
$line_color = "#D0D0D0";
$color = imagecolorallocate($this->img, hexdec(substr($line_color, 1, 2)), hexdec(substr($line_color, 3, 2)), hexdec(substr($line_color, 5, 2)));
for($i=0;$i<10;$i++){
$step =$line_height*$i+2;
imageline($this->img, 0, $step, $w,$step, $color);
}
$line_width = $w/10;
for($i=0;$i<10;$i++){
$step =$line_width*$i+2;
imageline($this->img, $step-2, 0, $step+2,$h, $color);
}
}
/**
* 画矩形边框
*/
private function createRec() {
imagerectangle($this->img, 0, 0, $this->width - 1, $this->height - 1, $this->fontColor);
} /**
* 写入验证码文字
*/
private function createFont() {
$this->createCode();
$color = $this->fontColor;
if (!empty($color)) {
$fontColor = imagecolorallocate($this->img, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)));
}
$x = ($this->width - 10) / $this->codeLen;
for ($i = 0; $i < $this->codeLen; $i++) {
if (empty($color)) {
$fontColor = imagecolorallocate($this->img, mt_rand(50, 155), mt_rand(50, 155), mt_rand(50, 155));
}
imagettftext($this->img, $this->fontSize, mt_rand(- 30, 30), $x * $i + mt_rand(6, 10), mt_rand($this->height / 1.3, $this->height - 5), $fontColor, $this->font, $this->code [$i]);
}
$this->fontColor = $fontColor;
} /**
* 画线
*/
private function createPix() {
$pix_color = $this->fontColor;
for ($i = 0; $i < 50; $i++) {
imagesetpixel($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
} for ($i = 0; $i < 2; $i++) {
imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
}
//画圆弧
for ($i = 0; $i < 1; $i++) {
// 设置画线宽度
// imagesetthickness($this->img, mt_rand(1, 3));
imagearc($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height)
, mt_rand(0, 160), mt_rand(0, 200), $pix_color);
}
imagesetthickness($this->img, 1);
} /**
* 显示验证码
*/
public function show() {
header("Content-type:image/png");
imagepng($this->img);
imagedestroy($this->img);
exit;
} /**
* 验证GD库是不否打开imagepng函数是否可用
*/
private function checkGD() {
return extension_loaded('gd') && function_exists("imagepng");
}
}
然后再控制器中调用就可以了。

最后,提醒大家记得开启在自动加载文件中session哦。

CI框架中,扩展验证码类。的更多相关文章
- CodeIgniter(CI)框架中的验证码
在CodeIgniter框架中,CI本身自带了验证码,但是查看文档的时候,发现: 需要新建一个表,用来存储验证码信息.因为习惯了session存储验证码信息,所以我把我认为比较好看的验证码应用在了CI ...
- 对CI框架中几个文件libraries
对CI框架中几个文件libraries,helpers,hooks夹说明 来源:未知 时间:2014-10-20 11:37 阅读数:117 作者:xbdadmin [导读] 1.lib ...
- CI 框架中的自定义路由规则
在 CI 框架中,一个 URL 和它对应的控制器中的类以及类中的方法是一一对应的,如: www.test.com/user/info/zhaoyingnan 其中 user 对应的就是控制器中的 us ...
- (转载)OC学习篇之---Foundation框架中的NSArray类和NSMutableArray类
在之前的一篇文章中介绍了Foundation框架中的NSString类和NSMutableString类,今天我们继续来看一下Foundation框架中的NSArray类和NSMutableArray ...
- php CI框架中URL特殊字符处理与SQL注入隐患
php CI框架中URL特殊字符处理与SQL注入隐患 php CI框架中URL特殊字符有很多是不支持的,导致像c++,括号这些常用的分类,字符都无法正常显示很头痛,而在配置里增加单引号' 反斜杠\ 这 ...
- php json_encode在CI框架中的使用细节
这个错误的造成原因是加载类类库,转换成json格式的时候不熟悉CI框架的规定导致的,CI框架中规定在将数据转换成json格式的时候需要将类库小写,当然了,调用的时候必须保证有这个类库,且可以在对应的文 ...
- (转载)OC学习篇之---Foundation框架中的其他类(NSNumber,NSDate,NSExcetion)
前一篇说到了Foundation框架中的NSDirctionary类,这一一篇来看一下Foundation的其他常用的类:NSNumber,NSDate,NSException. 注:其实按照Java ...
- (转载)OC学习篇之---Foundation框架中的NSDirctionary类以及NSMutableDirctionary类
昨天学习了Foundation框架中NSArray类和NSMutableArray类,今天来看一下Foundation框架中的NSDirctionary类,NSMutableDirctionary类, ...
- CI框架中集成CKEditor编辑器的教程
CKEditor是在很多开发过程中都会用到的一个富文本编辑器,那么如何在CI框架中使用它呢?这里介绍了在CI下使用CKEditor的方法,版本比较低,是在CI 1.7.3下使用fckeditor 2. ...
随机推荐
- 【BZOJ4826】[Hnoi2017]影魔 单调栈+扫描线
[BZOJ4826][Hnoi2017]影魔 Description 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝 ...
- 使用jdk中keytool生成证书
-genkey 在用户主目录中创建一个默认文件".keystore",还会产生一个mykey的别名,mykey中包含用户的公钥.私钥和证书 -alias 产生别名 -keystor ...
- MongoDB API java的使用
1.创建一个MongoDB数据库连接对象,它默认连接到当前机器的localhost地址,端口是27017. Mongo mongo=new Mongo(); 2.获得与某个数据库(例如“test”)的 ...
- 九度OJ 1076:N的阶乘 (数字特性、大数运算)
时间限制:3 秒 内存限制:128 兆 特殊判题:否 提交:6384 解决:2238 题目描述: 输入一个正整数N,输出N的阶乘. 输入: 正整数N(0<=N<=1000) 输出: 输入可 ...
- ABAP div / mod的用法
1.divdiv是用于取两数相除的商的,c = a div b,得到的c的值就是a除b的商.2.// 是用于取两数相除的结果的.c = a / b,如果c是i数据类型的,这个语法会进行四舍五入的.3. ...
- ThreadLocalMap里Entry声明为WeakReference
Java里,每个线程都有自己的ThreadLocalMap,里边存着自己私有的对象.Map的Entry里,key为ThreadLocal对象,value即为私有对象T.在spring MVC中,常用T ...
- debian7 amd64版本添加对x86包的支持
dpkg --add-architecture i386apt-get updateapt-get install ia32-libs ia32-libs-gtk
- python增删改查zabbix主机等
摘自: http://www.jianshu.com/p/e087cace8ddf 一.API简介 Zabbix API是在1.8版本中开始引进并且已经被广泛应用.所有的Zabbix移动客户端都是基于 ...
- RQNOJ 202 奥运火炬登珠峰:01背包
题目链接:https://www.rqnoj.cn/problem/202 题意: 登珠峰需要携带a(L)O2和t(L)N2. 有n个气缸可供选择.其中第i个气缸能装下a[i](L)O2和t[i](L ...
- c# 实现WebSocket
用C# ASP.NET MVC 实现WebSocket ,对于WebSocket想必都很了解了,不多说. 东西做的很粗糙 只能实现基本的聊天功能,不过基本的通信实现了,那么后序的扩展应该也不难(个人这 ...