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制作静态网站的模板框架(一)
Oct 09 PHP
php定时删除文件夹下文件(清理缓存文件)
Jan 23 PHP
php 修改、增加xml结点属性的实现代码
Oct 22 PHP
PHP字符串比较函数strcmp()和strcasecmp()使用总结
Nov 19 PHP
php无限分类使用concat如何实现
Nov 05 PHP
详解WordPress开发中的get_post与get_posts函数使用
Jan 04 PHP
支付宝支付开发――当面付条码支付和扫码支付实例
Nov 04 PHP
Yii2实现UploadedFile上传文件示例
Feb 15 PHP
微信公众号开发之获取位置信息php代码
Jun 13 PHP
php对象工厂类完整示例
Aug 09 PHP
Laravel5.0+框架邮件发送功能实现方法图文与实例详解
Apr 23 PHP
PHP商品秒杀问题解决方案实例详解【mysql与redis】
Jul 22 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面向对象全攻略 (十六) 对象的串行化
2009/09/30 PHP
PHP实现支持GET,POST,Multipart/form-data的HTTP请求类
2014/09/24 PHP
ThinkPHP写数组插入与获取最新插入数据ID实例
2014/11/03 PHP
js 新浪的一个图片播放图片轮换效果代码
2008/07/15 Javascript
Js 刷新框架页的代码
2010/04/13 Javascript
Javascript页面添加到收藏夹的简单方法
2013/08/07 Javascript
javascript中拼接HTML字符串的最快、最好的方法
2014/06/07 Javascript
让JavaScript的Alert弹出框失效的方法禁止弹出警告框
2014/09/03 Javascript
用javascript关闭本窗口技巧小结
2014/09/05 Javascript
使用javascript实现简单的选项卡切换
2015/01/09 Javascript
JavaScript中实现单体模式分享
2015/01/29 Javascript
js实现简单秒表走动的时钟特效
2020/03/25 Javascript
JS组件Bootstrap实现下拉菜单效果代码
2016/04/26 Javascript
Vue数据驱动模拟实现5
2017/01/13 Javascript
AngularJs定时器$interval 和 $timeout详解
2017/05/25 Javascript
zTree获取当前节点的下一级子节点数实例
2017/09/05 Javascript
详解angularJS+Ionic移动端图片上传的解决办法
2017/09/13 Javascript
JSONP原理及应用实例详解
2018/09/13 Javascript
详解vantUI框架在vue项目中的应用踩坑
2018/12/06 Javascript
Vue项目中ESlint规范示例代码
2019/07/04 Javascript
token 机制和实现方式
2020/12/15 Javascript
初步讲解Python中的元组概念
2015/05/21 Python
Python3使用正则表达式爬取内涵段子示例
2018/04/22 Python
Pycharm设置界面全黑的方法
2018/05/23 Python
selenium+python实现1688网站验证码图片的截取功能
2018/08/14 Python
numpy向空的二维数组中添加元素的方法
2018/11/01 Python
python 实现一个反向单位矩阵示例
2019/11/29 Python
比驿:全球酒店比价网
2018/06/20 全球购物
漫威玩具服装及周边商品官方购物网站:Marvel Shop
2019/05/11 全球购物
大专学生推荐信范文
2013/11/19 职场文书
大学生个人事迹材料
2014/01/21 职场文书
课程改革实施方案
2014/03/16 职场文书
二人合伙经营协议书
2014/09/13 职场文书
2014国庆节幼儿园亲子活动方案
2014/09/16 职场文书
政风行风自查自纠报告
2014/10/21 职场文书
浅析Python实现DFA算法
2021/06/26 Python