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往windows中添加用户
Dec 06 PHP
PHP 变量定义和变量替换的方法
Jul 30 PHP
php页面消耗内存过大的处理办法
Mar 18 PHP
php对二维数组按指定键值key排序示例代码
Nov 26 PHP
浅析ThinkPHP中execute和query方法的区别
Jun 13 PHP
C/S和B/S两种架构区别与优缺点分析
Oct 23 PHP
PHP中判断文件存在使用is_file还是file_exists?
Apr 03 PHP
PHP加密解密类实例代码
Jul 20 PHP
Yii2中Restful API原理实例分析
Jul 25 PHP
php简单处理XML数据的方法示例
May 19 PHP
PHP判断json格式是否正确的实现代码
Sep 20 PHP
php实现小程序支付完整版
Oct 09 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开发GUI
2006/10/09 PHP
PHP array 的加法操作代码
2010/07/24 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(三)
2014/06/23 PHP
php使用正则表达式获取图片url的方法
2015/01/16 PHP
PHP中trim()函数简单使用指南
2015/04/16 PHP
Yii框架扩展CGridView增加导出CSV功能的方法
2017/05/24 PHP
jQuery UI的Dialog无法提交问题的解决方法
2011/01/11 Javascript
jQuery中innerWidth()方法用法实例
2015/01/19 Javascript
JavaScript 匿名函数和闭包介绍
2015/04/13 Javascript
AngularJS 使用 UI Router 实现表单向导
2016/01/29 Javascript
完美解决js传递参数中加号和&amp;号自动改变的方法
2016/10/11 Javascript
微信小程序的日期选择器的实例详解
2017/09/29 Javascript
JS实现的文字间歇循环滚动效果完整示例
2018/02/13 Javascript
bootstrap中selectpicker下拉框使用方法实例
2018/03/22 Javascript
详解关于Vuex的action传入多个参数的问题
2019/02/22 Javascript
Vue infinite update loop的问题解决
2019/04/23 Javascript
Vuex新手的理解与使用详解
2019/05/31 Javascript
JavaScript中的函数申明、函数表达式、箭头函数
2019/12/06 Javascript
JS实现的定时器展示简单秒表、页面弹框及跳转操作完整示例
2020/01/26 Javascript
antd form表单数据回显操作
2020/11/02 Javascript
JavaScript中的Proxy对象
2020/11/27 Javascript
Python使用Dijkstra算法实现求解图中最短路径距离问题详解
2018/05/16 Python
解决python 文本过滤和清理问题
2019/08/28 Python
django数据模型中null和blank的区别说明
2020/09/02 Python
Python爬虫实战案例之爬取喜马拉雅音频数据详解
2020/12/07 Python
Python实现王者荣耀自动刷金币的完整步骤
2021/01/22 Python
Darphin迪梵官网: 来自巴黎,植物和精油调制的护肤品牌
2016/10/11 全球购物
哥伦比亚加拿大官网:Columbia Sportswear Canada
2020/09/07 全球购物
新闻网站实习自我鉴定
2013/09/25 职场文书
房地产销售经理岗位职责
2014/01/01 职场文书
爱国主义演讲稿
2014/05/07 职场文书
服务行业演讲稿
2014/09/02 职场文书
12.4法制宣传日标语
2014/10/08 职场文书
2014年护理工作总结范文
2014/11/14 职场文书
争先创优个人总结
2015/03/04 职场文书
Windows Server 2012 R2服务器安装与配置的完整步骤
2022/07/15 Servers