溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在php中使用ci框架實現(xiàn)一個驗證碼功能

發(fā)布時間:2021-04-13 16:15:22 來源:億速云 閱讀:190 作者:Leah 欄目:開發(fā)技術(shù)

怎么在php中使用ci框架實現(xiàn)一個驗證碼功能?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

php代碼:

<?php 
class Captcha_code
{
 var $width='60';
 var $num='4';
 var $height='20';
 var $name='randcode';
 public function __construct($conf="")
 {
  if($conf!="")
  {
   foreach($conf as $key=>$value)
   {
    $this->$key=$value;
   }
  }
 }
 function show()
 {
  Header("Content-type: image/gif");
  /*
  * 初始化
  */
  $border = 0; //是否要邊框 1要:0不要
  $how = $this->num; //驗證碼位數(shù)
  $w = $this->width; //圖片寬度
  $h = $this->height; //圖片高度
  $fontsize = 5; //字體大小
  $alpha = "abcdefghijkmnopqrstuvwxyz"; //驗證碼內(nèi)容1:字母
  $number = "023456789"; //驗證碼內(nèi)容2:數(shù)字
  $randcode = ""; //驗證碼字符串初始化
  srand((double)microtime()*1000000); //初始化隨機數(shù)種子
  $im = ImageCreate($w, $h); //創(chuàng)建驗證圖片
  /*
  * 繪制基本框架
  */
  $bgcolor = ImageColorAllocate($im, 255, 255, 255); //設(shè)置背景顏色
  ImageFill($im, 0, 0, $bgcolor); //填充背景色
  if($border)
  {
   $black = ImageColorAllocate($im, 0, 0, 0); //設(shè)置邊框顏色
   ImageRectangle($im, 0, 0, $w-1, $h-1, $black);//繪制邊框
  }
  /*
  * 逐位產(chǎn)生隨機字符
  */
  for($i=0; $i<$how; $i++)
  {   
   $alpha_or_number = mt_rand(0, 1); //字母還是數(shù)字
   $str = $alpha_or_number ? $alpha : $number;
   $which = mt_rand(0, strlen($str)-1); //取哪個字符
   $code = substr($str, $which, 1); //取字符
   $j = !$i ? 4 : $j+15; //繪字符位置
   $color3 = ImageColorAllocate($im, mt_rand(0,100), mt_rand(0,100), mt_rand(0,100)); //字符隨即顏色
   ImageChar($im, $fontsize, $j, 3, $code, $color3); //繪字符
   $randcode .= $code; //逐位加入驗證碼字符串
  }
  /*
  * 添加干擾
  */
  for($i=0; $i<5; $i++)//繪背景干擾線
  {   
   $color1 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); //干擾線顏色
   ImageArc($im, mt_rand(-5,$w), mt_rand(-5,$h), mt_rand(20,300), mt_rand(20,200), 55, 44, $color1); //干擾線
  }   
  for($i=0; $i<$how*15; $i++)//繪背景干擾點
  {   
   $color2 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); //干擾點顏色 
   ImageSetPixel($im, mt_rand(0,$w), mt_rand(0,$h), $color2); //干擾點
  }
  //把驗證碼字符串寫入session
  //$this->session->set_userdata(array($this->name=>$randcode));
  $_SESSION[$this->name]=$randcode;
  /*繪圖結(jié)束*/
  Imagegif($im);
  ImageDestroy($im);
  /*繪圖結(jié)束*/
 }
}
?>


調(diào)用php代碼:

function verify_image() {
        $conf['name'] = 'verify_code'; //作為配置參數(shù)
        $this->load->library('lib_captcha', $conf);
        $this->lib_captcha->show();
        $yzm_session = $this->session->userdata('verify_code');
        echo $yzm_session;
    }


html代碼:

<dl>
          <dt>驗證碼:</dt>
          <dd>
            <input type="text" name="verify_text" id="verify_text" class="yzma" value="">
            <img src="/user/verify_image" alt="驗證碼" id="verify_code" class="yz_img" />
            <a href="javascript:changeCode();" class="change_yz">換一張</a>
          </dd>
          <dd class="tips_wrong"><b>驗證碼不正確</b></dd>
          <dd class="tips_correct"></dd>
        </dl>


js代碼:

<script type="text/javascript">
    function changeCode(){
         FS.query("#verify_code").src ="/user/verify_image?r=" + Math.random();
    }
</script>

關(guān)于怎么在php中使用ci框架實現(xiàn)一個驗證碼功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI