使用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. .Net反射机制

    现在谈.Net反射机制本不在计划中,因为本打算研究完设计模式后再去学习别的技术:但迫于设计模式系列一创建型之(抽象工厂模式)一章中遗留问题,才不得已在设计模式之游中插入本篇文章!签于本人对反射理解也不 ...

  2. Spring Boot:Thymeleaf篇

    Spring Boot干货系列:(四)Thymeleaf篇http://www.cnblogs.com/zheting/p/6707037.html 前言 Web开发是我们平时开发中至关重要的,这里就 ...

  3. 为什么需要onRoute函数?

    为什么需要onRoute函数? · Tencent/wepy Wiki https://github.com/Tencent/wepy/wiki/%E4%B8%BA%E4%BB%80%E4%B9%88 ...

  4. argument python 参数 举例

    举例 例1:def multipute(x,y): x = 2 y[0] = ['spam'] return x,y X = 1 L = [1,2] X,L = multipute(X, L) pri ...

  5. SD 相关表

    一.客户主数据基本数据放在KNA1里:公司代码放在KNB1里:销售视图放在KNVV里:合作伙伴放在KNVP里: 二.信用主数据KNKK里有信贷限额.应收总额.特别往来:S066里是未清订单值:S067 ...

  6. linux复制和移动

    复制: -f  强制覆盖同名文件 -r  按递归方式保留原目录结构复制文件 cp -Rf /home/user1/*   /root/temp/ 将/home/user1目录下的所有东西拷到/root ...

  7. python优缺点小结

    优点:  1.语言简洁优美 例如去除了大括号,写法简单,写法更接近于英语,其他语言几十上百行的代码,十来行就能解决,而且还好看 2.跨平台,window.linux.mac通用 3.排行高,社区完善 ...

  8. openocd+jlink为mini2440调试u-boot

    需要安装openocd,如果已经安装了系统默认的openocd(默认是0.5.0,版本太低),需要先卸载掉. 在安装前需要安装必需的一些库文件: -dev libusb-1.0-0 automake ...

  9. 排序List集合

    这两天写代码过程中遇到一个需求,需要按照某个字段排序List集合,自己实现了一半,发现网上有个更好的版本,就采用了这个,记录下来. 使用这个工具类要注意一个就是 如果你按照age 字段排序,那么age ...

  10. python类初探

    class human: is_alive=True is_man=True def __init__(self,age): print('this is __init__() method, whi ...