全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

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


# CodeIgniter  # 框架  # 验证码  # 类库  # CI框架验证码CAPTCHA辅助函数用法实例  # php ci框架验证码实例分析  # CI框架常用经典操作类总结(路由  # 伪静态  # 分页  # session  # 验证码等)  # CodeIgniter表单验证方法实例详解  # Codeigniter实现处理用户登录验证后的URL跳转  # CI框架表单验证实例详解  # CI框架教程之优化验证码机制详解【验证码辅助函数】  # 程序设计  # 进阶  # 是在  # 相关内容  # 有个  # 感兴趣  # 给大家  # 几位  # 更多关于  # 不正确  # 所述  # 创建一个  # 正常运行  # 面向对象  # 是否正确  # 背景色  # 四五个  # 操作技巧 


相关文章: 制作农业网站的软件,比较好的农业网站推荐一下?  ,在苏州找工作,上哪个网站比较好?  阿里云网站搭建费用解析:服务器价格与建站成本优化指南  金*站制作公司有哪些,金华教育集团官网?  已有域名建站全流程解析:网站搭建步骤与建站工具选择  建站主机系统SEO优化与智能配置核心关键词操作指南  利用JavaScript实现拖拽改变元素大小  青岛网站设计制作公司,查询青岛招聘信息的网站有哪些?  如何在宝塔面板中创建新站点?  网站视频制作书签怎么做,ie浏览器怎么将网站固定在书签工具栏?  胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?  如何做网站制作流程,*游戏网站怎么搭建?  如何在IIS管理器中快速创建并配置网站?  公司网站设计制作厂家,怎么创建自己的一个网站?  如何快速搭建高效服务器建站系统?  建站之星代理如何获取技术支持?  台州网站建设制作公司,浙江手机无犯罪记录证明怎么开?  如何在宝塔面板中修改默认建站目录?  大连 网站制作,大连天途有线官网?  C#如何序列化对象为XML XmlSerializer用法  如何在万网开始建站?分步指南解析  如何用y主机助手快速搭建网站?  c++23 std::expected怎么用 c++优雅处理函数错误返回【详解】  简单实现Android文件上传  企业在线网站设计制作流程,想建设一个属于自己的企业网站,该如何去做?  高性能网站服务器部署指南:稳定运行与安全配置优化方案  建站之星安全性能如何?防护体系能否抵御黑客入侵?  如何获取上海专业网站定制建站电话?  如何注册花生壳免费域名并搭建个人网站?  如何快速搭建高效可靠的建站解决方案?  制作无缝贴图网站有哪些,3dmax无缝贴图怎么调?  如何在自有机房高效搭建专业网站?  建站主机默认首页配置指南:核心功能与访问路径优化  学校免费自助建站系统:智能生成+拖拽设计+多端适配  建站上传速度慢?如何优化加速网站加载效率?  营销式网站制作方案,销售哪个网站招聘效果最好?  建站之星如何快速生成多端适配网站?  香港服务器建站指南:外贸独立站搭建与跨境电商配置流程  ui设计制作网站有哪些,手机UI设计网址吗?  品牌网站制作公司有哪些,买正品品牌一般去哪个网站买?  nginx修改上传文件大小限制的方法  深圳 网站制作,深圳招聘网站哪个比较好一点啊?  韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南  如何快速重置建站主机并恢复默认配置?  如何实现建站之星域名转发设置?  广德云建站网站建设方案与建站流程优化指南  电脑免费海报制作网站推荐,招聘海报哪个网站多?  东莞专业制作网站的公司,东莞大学生网的网址是什么?  如何快速生成ASP一键建站模板并优化安全性?  jQuery 常见小例汇总 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。