CodeIgniter框架验证码类库文件与用法示例
程序员文章站
2024-03-13 14:54:57
本文实例讲述了codeigniter框架验证码类库文件与用法。分享给大家供大家参考,具体如下:
折腾了我四五个小时,终于,ci的验证码类库成功的整出来了。
下面请看源码...
本文实例讲述了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程序设计有所帮助。