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程序设计有所帮助。

时间: 2024-08-02 18:26:25

CodeIgniter框架验证码类库文件与用法示例的相关文章

CodeIgniter框架基本增删改查操作示例

本文实例讲述了CodeIgniter框架基本增删改查操作.分享给大家供大家参考,具体如下: 对于codeigniter的增删改,在此我用自己的一个例子来说明一下: 创建数据库: CREATE TABLE IF NOT EXISTS `users` ( `id` int(10) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, `email` varchar(100)

CI框架实现优化文件上传及多文件上传的方法

本文实例分析了CI框架实现优化文件上传及多文件上传的方法.分享给大家供大家参考,具体如下: 最近一直在研究Codeigniter框架,开发项目写到文件上传的时候发现大部分程序员使用Codeigniter框架的文件上传类编写上传方法的时候写的都存在这代码冗余(或者说代码重复利用率低.比较消耗资源.)故而我研究出一个稍微优化一点的上传方法.并且在查找资料时发现,Codeigniter框架同时上传多个文件比较困难,所以在优化方法的同时我又研究了一下如何使用Codeigniter框架实现同时上传多个文件

PHP实现的多文件上传类及用法示例_php技巧

本文实例讲述了PHP实现的多文件上传类及用法.分享给大家供大家参考,具体如下: 1.upFiles.css.php 文件 <?php class UploadFiles{ private $maxsize = '1000000'; //允许上传文件最大长度 private $allowtype = array('jpg','png','gif','jpeg');//允许上传文件类型 private $israndfile = true;//是否随机文件名 private $filepath;//

PHP判断文件是否被引入的方法get_included_files用法示例_php技巧

本文实例讲述了PHP判断文件是否被引入的方法get_included_files用法.分享给大家供大家参考,具体如下: <?php // 本文件是 abc.php include 'test1.php'; include_once 'test2.php'; require 'test3.php'; require_once 'test4.php'; $included_files = get_included_files(); foreach ($included_files as $filen

PHP实现导出excel数据的类库用法示例_php技巧

本文实例讲述了PHP实现导出excel数据的类库用法.分享给大家供大家参考,具体如下: 今天一个项目要做一个PHP导出数据用excel保存,在网上找到一个本来是想用phpexcel的,后来发现太难了,就换了一个但导出的歌声是XML 类写的很简单,但很实用.只能简单的导出字符串和数字二种格式. 如果你有兴趣,你可以拿去扩充了,基本够用. class Excel_XML { //定于私有变量,顶部标签 private $header = "<?xml version=\"1.0\&q

CodeIgniter框架常见用法工作总结

本文实例讲述了CodeIgniter框架常见用法.分享给大家供大家参考,具体如下: 1.codeigniter控制器超级对象和属性 $this->load; $this->load->database(); $this->load->view(); $this->load->helper(); $this->uri; $this->uri->segment(3); $this->input; 2.数据库配置 $this->load-&

使用 CodeIgniter 框架快速开发 PHP 应用(三)

原文:使用 CodeIgniter 框架快速开发 PHP 应用(三)分析网站结构 既然我们已经安装 CI ,我们开始了解它如何工作. 读者已经知道 CI 实现了MVC式样. 通过对目录和文件的内容进行分类, 而不是让代码大块大块地纠集在一起. 这一章,我们将会对 MVC 理论做个简短的介绍, 然后再介绍 CI 的MVC实现方式.特别地,要了解那些目录和文件如何互相交换信息?网站结构是怎样的?以及CI是如何自如地动作于其中的? 这一章将会介绍: .MVC 如何架构一个动态网站 .CI如何接收和分析

PHP CodeIgniter框架的工作原理研究

 这篇文章主要介绍了PHP CodeIgniter框架的工作原理研究,本文首先分析了它的工作流程,然后总结了它的工作原理,需要的朋友可以参考下     CodeIgniter(以下简称CI,官网以及中国站)是一个流行的PHP框架,小巧但功能强大,简洁轻量同时拥有很好的扩展性,在国内也比较受欢迎.另一方面,CI却没有与时俱进,并不支持PHP5.3之后的一些特性,导致它相对更适合较老一些的项目.虽然如此,CI仍是一个优秀的框架,而且它本身内核较小,源码优雅,适于学习. CI易于使用,可以方便的开发出

JavaScript框架、类库、工具汇总

相比于JavaScript开发人员的数量,目前JavaScript框架.类库和工具的数量似乎更多一些.截至2017年5月,GitHub上的快速搜索显示,有超过110万个JavaScript项目.npmjs.org有50万个可用的软件包,每月下载量近100亿次. 本文将会讨论目前最为流行的客户端JavaScript框架.类库和工具以及它们之间的基本差异.也许本文无法告诉你哪个是最好的,但是最适合自己项目的,就是最好的. 类库 类库是一个有组织的功能集合.典型的类库包括字符串处理.日期.HTML D