CI 驗證碼 Captcha (含點圖刷新)

Model : test.php
PHP 复制代码
 
//輸出驗證碼圖片
public  function get_captcha ( ) {
  //rand()取亂數後轉為md5碼,並只取前四碼,str用來小寫轉大寫
  $rand  =  strtoupper ( substr ( md5 ( rand ( ) ) , 0 , 4 ) ) ;
  $session_rand  =  array ( "rand" => $rand ) ;  //存入陣列
  $this -> session -> set_userdata ( $session_rand ) ;  //紀錄 session
 
  $img  =  array (
   'word'                 =>  $rand ,
   'img_path'         =>  './captcha/' ,
   'img_url'          => base_url ( 'captcha' ) . '/' ,  //請先建好權限777的 captcha 資料夾
   'font_path'          =>  './path/arial.ttf' ,  //設置字體,避免跑版
   'img_width'         =>  '100' ,
   'expiration'         =>  10  //設定圖片刪除時間 = 10秒
  ) ;
 
  $rec  = create_captcha ( $img ) ;
  return  $rec [ 'image' ] ;  //輸出img驗證圖片
}
 
复制代码


Controller : captcha.php
PHP 复制代码
 
class Captcha  extends CI_Controller  {
  function __construct ( )  {
  parent ::__construct ( ) ;
   $this -> load -> model ( 'test' ) ; //載入模型
   $this -> load -> helper ( 'captcha' ) ;         //載入驗證碼函式
  }
 
  public  function index ( ) {
   echo  $this -> test -> get_captcha ( ) ;
  }
}
 
复制代码


Controller : login.php
PHP 复制代码
 
class Login  extends CI_Controller  {
  function __construct ( )  {
  parent ::__construct ( ) ;
   $this -> load -> model ( 'test' ) ;
   $this -> load -> helper ( 'captcha' ) ;         //載入驗證碼函式
  }
 
  public  function index ( ) {
   $data [ 'img' ]  =  $this -> test -> get_captcha ( ) ;
   $this -> load -> view ( 'login' ,  $data ) ;
  }
}
 
复制代码


view : login.php
HTML 复制代码
 
<script type="text/javascript">
function load_captcha(id,url){
 $("#"+id).html('');
 $("#"+id).load(url); 
}
</script>
 
<input type="text" name="s" placeholder="輸入帳號" /><br />
<input type="password" name="p" placeholder="輸入密碼" /><br />
<input type="text" name="r" placeholder="輸入驗證碼" /><br />
<input type="submit" value="登入"> 
<a href="#" onclick="load_captcha('captcha','<?php echo site_url('captcha');?>');" title="換一張" id="captcha" > <?php echo $img;?></a>
 
复制代码


第一次讀取控制器 login.php
引用於 model 內的方法

點圖後刷新
是刷新控制器 captcha.php
一樣是引用 model 同一個方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值