您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“CodeIgniter框架驗(yàn)證碼類庫(kù)文件怎么用”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“CodeIgniter框架驗(yàn)證碼類庫(kù)文件怎么用”這篇文章吧。
在application/libraries建立Authcode.php文件,代碼如下:
<?php class Authcode { var $CI; var $fontPath;//字體路徑 var $image; var $charLen = 4; //生成幾位驗(yàn)證碼 var $arrChr = array();//驗(yàn)證碼字符 var $width = 83; //圖片寬 var $height = 24; //圖片高 var $bgcolor = "#ffffff"; //背景色 var $showNoisePix = true; //生成雜點(diǎn) var $noiseNumPix = 80; //生成雜點(diǎn)數(shù)量 var $showNoiseLine = true; //生成雜線 var $noiseNumLine = 2; //生成雜線數(shù)量 var $showBorder = true; //邊框,當(dāng)雜點(diǎn)、線一起作用的時(shí)候,邊框容易受干擾 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'));//數(shù)字字母驗(yàn)證碼 //$this->arrChr = range('A', 'Z');//純字母驗(yàn)證碼 $this->arrChr = range(0, 9);//純數(shù)字驗(yàn)證碼 } /** * 顯示驗(yàn)證碼 * */ 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); } /** * 顯示驗(yàn)證碼的JS調(diào)用 * */ function showScript() { //顯示驗(yàn)證碼 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=\"點(diǎn)擊更換圖片\">');"; } /** * 檢查驗(yàn)證碼是否正確 * * @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代碼結(jié)束
在Controller中,有個(gè)admin類,其中有兩個(gè)方法:
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 '驗(yàn)證碼不正確,請(qǐng)重新輸入'; } }else{ $this->load->view('demo'); } } function show_captcha(){ //此方法用于顯示驗(yàn)證碼圖片,歸一個(gè)view中的img的src調(diào)用 $this->authcode->show(); } }
下面是在視圖view中創(chuàng)建一個(gè)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="驗(yàn)證" /> <?php echo form_close();?>
OK. 一切結(jié)束,終于正常運(yùn)行了。
以上是“CodeIgniter框架驗(yàn)證碼類庫(kù)文件怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。