CodeIgniter框架验证码类库文件与用法示例


Posted in PHP onMarch 18, 2017

本文实例讲述了CodeIgniter框架验证码类库文件与用法。分享给大家供大家参考,具体如下:

折腾了我四五个小时,终于,ci的验证码类库成功的整出来了。

下面请看源码:

在application/libraries建立Authcode.php文件,代码如下:

<?php
class Authcode
{
 var $CI;
 var $fontPath;//字体路径
 var $image;
 var $charLen   = 4; //生成几位验证码
 var $arrChr   = array();//验证码字符
 var $width    = 83; //图片宽
 var $height   = 24; //图片高
 var $bgcolor   = "#ffffff"; //背景色
 var $showNoisePix  = true; //生成杂点
 var $noiseNumPix  = 80; //生成杂点数量
 var $showNoiseLine  = true; //生成杂线
 var $noiseNumLine  = 2; //生成杂线数量
 var $showBorder  = true; //边框,当杂点、线一起作用的时候,边框容易受干扰
 var $borderColor  = "#000000";
 function Authcode()
 {
  $this->CI = & get_instance();
  $this->fontPath = realpath(dirname(__FILE__) . '/fonts/'); //字体文件
  //$this->arrChr   = array_merge(range(1, 9) , range('A', 'Z'));//数字字母验证码
  //$this->arrChr   = range('A', 'Z');//纯字母验证码
  $this->arrChr = range(0, 9);//纯数字验证码
 }
 /**
  * 显示验证码
  *
  */
 function show()
 {
  $this->image = imageCreate($this->width, $this->height);
  $this->back = $this->getColor($this->bgcolor);
  imageFilledRectangle($this->image, 0, 0, $this->width, $this->height, $this->back);
  $size = $this->width / $this->charLen - 4;
  if ($size > $this->height) {
   $size = $this->height;
  }
  $left = ($this->width - $this->charLen * ($size + $size / 10)) / $size + 5;
  $code = '';
  for($i = 0; $i < $this->charLen; $i ++) {
   $randKey = rand(0, count($this->arrChr) - 1);
   $randText = $this->arrChr[$randKey];
   $code .= $randText;
   $textColor = imageColorAllocate($this->image, rand(0, 100), rand(0, 100), rand(0, 100));
   $font = $this->fontPath . '/' . rand(1, 5) . ".ttf";
   $randsize = rand($size - $size / 10, $size + $size / 10);
   $location = $left + ($i * $size + $size / 10);
   @imagettftext($this->image, $randsize, rand(- 18, 18), $location, rand($size - $size / 10, $size + $size / 10) + 2, $textColor, $font, $randText);
  }
  if ($this->showNoisePix == true) {
   $this->setNoisePix();
  }
  if ($this->showNoiseLine == true) {
   $this->setNoiseLine();
  }
  if ($this->showBorder == true) {
   $this->borderColor = $this->getColor($this->borderColor);
   imageRectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $this->borderColor);
  }
  $this->CI->session->set_userdata('auth_code', $code);
  ob_clean();
  header("Content-type: image/jpeg");
  imagejpeg($this->image);
  imagedestroy($this->image);
 }
 /**
  * 显示验证码的JS调用
  *
  */
 function showScript()
 {
  //显示验证码
  echo "var img_src = '/imgauthcode/show/?';\n";
  echo "document.writeln('<img id=\"img_authcode\" src=\"' + img_src + Math.random() + '\" style=\"cursor:hand;\" onclick=\"this.src=img_src + Math.random();\" alt=\"点击更换图片\">');";
 }
 /**
  * 检查验证码是否正确
  *
  * @param string $auth_code
  * @return bool
  */
 function check($auth_code = null)
 {
  return ($this->CI->session->userdata('auth_code') && $auth_code) ? ($this->CI->session->userdata('auth_code') === $auth_code) : false;
 }
 function getColor($color)
 {
  $color = eregi_replace("^#", "", $color);
  $r = $color[0] . $color[1];
  $r = hexdec($r);
  $b = $color[2] . $color[3];
  $b = hexdec($b);
  $g = $color[4] . $color[5];
  $g = hexdec($g);
  $color = imagecolorallocate($this->image, $r, $b, $g);
  return $color;
 }
 function setNoisePix()
 {
  for($i = 0; $i < $this->noiseNumPix; $i ++) {
   $randColor = imageColorAllocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
   imageSetPixel($this->image, rand(0, $this->width), rand(0, $this->height), $randColor);
  }
 }
 function setNoiseLine()
 {
  for($i = 0; $i < $this->noiseNumLine; $i ++) {
   $randColor = imageColorAllocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
   imageline($this->image, rand(1, $this->width), rand(1, $this->height), rand(1, $this->width), rand(1, $this->height), $randColor);
  }
 }
}

Authcode.php代码结束

在Controller中,有个admin类,其中有两个方法:

Class Admin extends CI_Controller{
 function __construct()
 {
  parent::__construct();
  $this->load->library('Authcode');
 }
function captcha(){
  if($_POST){
    if ($this->authcode->check($this->input->post('gd_pic'))) {
    echo "right";
   } else {
    echo '验证码不正确,请重新输入';
   }
  }else{
   $this->load->view('demo');
  }
 }
 function show_captcha(){ //此方法用于显示验证码图片,归一个view中的img的src调用
  $this->authcode->show();
 }
}

下面是在视图view中创建一个demo.php了,代码如下:

<?php echo form_open('c=admin&m=captcha');?>
<input type="text" name="gd_pic" />
<img src="<?php echo base_url('?c=admin&m=show_captcha');?>" ><br>
<input type="submit" name="submit" value="验证" />
<?php echo form_close();?>

OK. 一切结束,终于正常运行了。

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

PHP 相关文章推荐
php下通过POST还是GET来传值
Jun 05 PHP
PHP缓存技术的使用说明
Aug 06 PHP
超小PHP小马小结(方便查找后门的朋友)
May 05 PHP
Zend的MVC机制使用分析(二)
May 02 PHP
深入PHP数据加密详解
Jun 18 PHP
ThinkPHP之M方法实例详解
Jun 20 PHP
Yii2分页的使用及其扩展方法详解
May 23 PHP
学习PHP session的传递方式
Jun 15 PHP
PHP Ajax实现无刷新附件上传
Aug 17 PHP
PHP基于单例模式编写PDO类的方法
Sep 13 PHP
Yii2数据库操作常用方法小结
May 04 PHP
Laravel框架生命周期与原理分析
Jun 12 PHP
YII框架批量插入数据的方法
Mar 18 #PHP
thinkPHP5.0框架URL访问方法详解
Mar 18 #PHP
thinkPHP5.0框架模块设计详解
Mar 18 #PHP
thinkPHP5.0框架命名空间详解
Mar 18 #PHP
thinkPHP5.0框架自动加载机制分析
Mar 18 #PHP
thinkPHP5.0框架引入Traits功能实例分析
Mar 18 #PHP
2017年最新PHP经典面试题目汇总(上篇)
Mar 17 #PHP
You might like
PHP JSON 数据解析代码
2010/05/26 PHP
laravel 框架结合关联查询 when()用法分析
2019/11/22 PHP
PHP扩展类型及安装方式解析
2020/04/27 PHP
JavaScript进阶教程(第四课第一部分)
2007/04/05 Javascript
javascript跨域刷新实现代码
2011/01/01 Javascript
eval与window.eval的差别分析
2011/03/17 Javascript
JavaScript call apply使用 JavaScript对象的方法绑定到DOM事件后this指向问题
2011/09/28 Javascript
js获取网页可见区域、正文以及屏幕分辨率的高度
2014/05/15 Javascript
jQuery判断复选框是否勾选的原理及示例
2014/05/21 Javascript
node.js读取文件到字符串的方法
2015/06/29 Javascript
javascript跨域方法、原理以及出现问题解决方法(详解)
2015/08/06 Javascript
JavaScript登录验证码的实现
2016/10/27 Javascript
JS小数转换为整数的方法分析
2017/01/07 Javascript
JS简单实现点击按钮或文字显示遮罩层的方法
2017/04/27 Javascript
详解extract-text-webpack-plugin 的使用及安装
2018/06/12 Javascript
Intellij IDEA搭建vue-cli项目的方法步骤
2018/10/20 Javascript
微信小程序云开发之模拟后台增删改查
2019/05/16 Javascript
原生javascript实现类似vue的数据绑定功能示例【观察者模式】
2020/02/24 Javascript
Openlayers实现地图的基本操作
2020/09/28 Javascript
Vue通过阿里云oss的url连接直接下载文件并修改文件名的方法
2020/12/25 Vue.js
[30:55]完美世界DOTA2联赛PWL S2 Magma vs LBZS 第二场 11.18
2020/11/18 DOTA
深入理解python中的atexit模块
2017/03/07 Python
python3.6、opencv安装环境搭建过程(图文教程)
2019/11/05 Python
如何基于Python获取图片的物理尺寸
2019/11/25 Python
基于YUV 数据格式详解及python实现方式
2019/12/09 Python
关于Python中定制类的比较运算实例
2019/12/19 Python
浅谈Tensorflow 动态双向RNN的输出问题
2020/01/20 Python
python GUI库图形界面开发之PyQt5信号与槽基本操作
2020/02/25 Python
Pycharm内置终端及远程SSH工具的使用教程图文详解
2020/03/19 Python
Ralph Lauren拉夫·劳伦美国官网:带有浓郁美国气息的高品味时装品牌
2017/11/01 全球购物
加拿大领先的冒险和户外零售商:Atmosphere
2017/12/19 全球购物
个人自我鉴定
2013/11/07 职场文书
个人简历自我评价
2014/01/06 职场文书
旺仔牛奶广告词
2014/03/20 职场文书
《他得的红圈圈最多》教学反思
2014/04/24 职场文书
2015年学校保卫部工作总结
2015/05/11 职场文书