Model : test.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
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
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
<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 同一個方法
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 同一個方法