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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了CodeIgniter框架验证码类库文件与用法示例脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

下面请看码:

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

<PRe class="brush: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); } } }

脚本宝典总结

以上是脚本宝典为你收集整理的CodeIgniter框架验证码类库文件与用法示例全部内容,希望文章能够帮你解决CodeIgniter框架验证码类库文件与用法示例所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。