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

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

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

下面请看源码:

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

rush:PHP;"> 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,$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),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,$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('\"点击更换图片\">');";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,255),255)); imagesetPixel($this->image,$this->width),$this->height),$randColor); } } function setNoiseLine() { for($i = 0; $i < $this->noiseNumLine; $i ++) { $randColor = imageColorAllocate($this->image,255)); imageline($this->image,rand(1,$randColor); } } }

Authcode.PHP代码结束

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

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了,代码如下:

rush:PHP;">

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

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


服务器优化必备:深入了解PHP8底层开发原理
Golang的网络编程:如何快速构建高性能的网络应用?
Golang和其他编程语言的对比:为什么它的开发效率更高?
PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用
将字符重新排列以形成回文(如果可能)在C++中
掌握PHP8底层开发原理和新特性:创建高效可扩展的应用程序
服务器性能优化必学:掌握PHP8底层开发原理
PHP8新特性和底层开发原理详解:优化应用性能的终极指南
将 C/C++ 代码转换为汇编语言
深入研究PHP8底层开发原理:创建高效可扩展的应用程序
C++程序查找法向量和迹
PHP8底层开发原理实战指南:提升服务器效能
重排数组,使得当 i 为偶数时,arr[i] >= arr[j],当 i 为奇数时,arr[i] <= arr[j],其中 j < i,使用 C++ 语言实现
Golang的垃圾回收:为什么它可以减少开发人员的负担?
C++程序:将一个数组的所有元素复制到另一个数组中
Golang:构建智能系统的基石
为什么AI开发者应该关注Golang?
在C和C++中,逗号(comma)的用法是用来分隔表达式或语句
PHP8底层开发原理解析及新特性应用实例
利用PHP8底层开发原理解析新特性:如何构建出色的Web应用