使用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框架中,扩展验证码类。的更多相关文章

  1. CodeIgniter(CI)框架中的验证码

    在CodeIgniter框架中,CI本身自带了验证码,但是查看文档的时候,发现: 需要新建一个表,用来存储验证码信息.因为习惯了session存储验证码信息,所以我把我认为比较好看的验证码应用在了CI ...

  2. 对CI框架中几个文件libraries

    对CI框架中几个文件libraries,helpers,hooks夹说明 来源:未知    时间:2014-10-20 11:37   阅读数:117   作者:xbdadmin [导读] 1.lib ...

  3. CI 框架中的自定义路由规则

    在 CI 框架中,一个 URL 和它对应的控制器中的类以及类中的方法是一一对应的,如: www.test.com/user/info/zhaoyingnan 其中 user 对应的就是控制器中的 us ...

  4. (转载)OC学习篇之---Foundation框架中的NSArray类和NSMutableArray类

    在之前的一篇文章中介绍了Foundation框架中的NSString类和NSMutableString类,今天我们继续来看一下Foundation框架中的NSArray类和NSMutableArray ...

  5. php CI框架中URL特殊字符处理与SQL注入隐患

    php CI框架中URL特殊字符处理与SQL注入隐患 php CI框架中URL特殊字符有很多是不支持的,导致像c++,括号这些常用的分类,字符都无法正常显示很头痛,而在配置里增加单引号' 反斜杠\ 这 ...

  6. php json_encode在CI框架中的使用细节

    这个错误的造成原因是加载类类库,转换成json格式的时候不熟悉CI框架的规定导致的,CI框架中规定在将数据转换成json格式的时候需要将类库小写,当然了,调用的时候必须保证有这个类库,且可以在对应的文 ...

  7. (转载)OC学习篇之---Foundation框架中的其他类(NSNumber,NSDate,NSExcetion)

    前一篇说到了Foundation框架中的NSDirctionary类,这一一篇来看一下Foundation的其他常用的类:NSNumber,NSDate,NSException. 注:其实按照Java ...

  8. (转载)OC学习篇之---Foundation框架中的NSDirctionary类以及NSMutableDirctionary类

    昨天学习了Foundation框架中NSArray类和NSMutableArray类,今天来看一下Foundation框架中的NSDirctionary类,NSMutableDirctionary类, ...

  9. CI框架中集成CKEditor编辑器的教程

    CKEditor是在很多开发过程中都会用到的一个富文本编辑器,那么如何在CI框架中使用它呢?这里介绍了在CI下使用CKEditor的方法,版本比较低,是在CI 1.7.3下使用fckeditor 2. ...

随机推荐

  1. Netty入门 - 秒懂

    目录 Netty 入门 前言: 建立项目 编写一个Discard Handler 处理器 编写一个Discard 服务器 线程组 启动帮助类 设置Channel 通道的选项 测试:发送消息到Disca ...

  2. 用 Apache POI 读取 XLSX 数据

    最近因为项目的原因,需要从一些 Microsoft Office Excel 文件读取数据并加载到数据库. Google了一下方法,发现其实可以用的 Java 第三方库很多,最著名的是 Apache ...

  3. [容易]在O(1)时间复杂度删除链表节点

    题目来源:http://www.lintcode.com/zh-cn/problem/delete-node-in-the-middle-of-singly-linked-list/

  4. Java AQS详解(转)

    原文地址 一.概述 谈到并发,不得不谈ReentrantLock:而谈到ReentrantLock,不得不谈AbstractQueuedSynchronizer(AQS)! 类如其名,抽象的队列式的同 ...

  5. 【windows】如何让一个程序开机自启动

    windows的开机自启动也是将一个程序放在文件夹下即可,将应用程序或者快捷方式放在如下文件夹下,即可实现开机自启动 C:\ProgramData\Microsoft\Windows\Start Me ...

  6. PAT 天梯赛 L3-003. 社交集群 【并查集】

    题目链接 https://www.patest.cn/contests/gplt/L3-003 思路 并查集 用一个 cou[i] 来表示 第 i 门课程 的第一个 感兴趣的人 并的时候 判断 cou ...

  7. Google IO 2019 Android 太长不看版

    Google I/O 2019, 这里有个playlist是所有Android开发相关的session视频合集: Android & Play at Google I/O 2019 当然啦每个 ...

  8. linux系统调用mount全过程分析【转】

    本文转载自:https://blog.csdn.net/skyflying2012/article/details/9748133 系统调用本身是软中断,使用系统调用,内核也陷入内核态,异常处理,找到 ...

  9. 在datax之前版本中添加filewriter并创建job时出现问题

    问题描述:

  10. MySQL Unable to convert MySQL date/time value to System.DateTime的解决办法

    在连接串中加入 Convert Zero Datetime=True