用户
 找回密码
 入住 CI 中国社区
搜索
查看: 6993|回复: 8
收起左侧

[讨论/交流] CI 驗證碼 Captcha (含點圖刷新)

[复制链接]
发表于 2014-11-18 16:19:45 | 显示全部楼层 |阅读模式
本帖最后由 Closer 于 2015-1-9 11:42 编辑

參考自:http://my.oschina.net/sorenring/blog/343462

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 同一個方法


 
发表于 2014-11-19 20:42:28 | 显示全部楼层
以前也自己写,现在改用第三方的了  自己只加个隐藏token做二次验证
极验验证    geetest点坑  可以看看  

以前自己写的只做备用了
 楼主| 发表于 2014-11-20 09:36:40 | 显示全部楼层
我還是比較不信任第三方的東西
哪天他的網站掛了你就得重弄
或是給你偷放個木馬都不知道

你說的那個還不錯
只是好像限制台灣無法使用
還是他的套件出了問題
我在他官網或有使用的網站
看不到那個拖拉條
发表于 2015-1-9 16:11:12 | 显示全部楼层
{:soso_e113:}谢谢  搞成了
发表于 2015-7-14 03:06:51 | 显示全部楼层
点图为什么不刷新呢,偶用的火狐
 楼主| 发表于 2015-7-14 11:35:09 | 显示全部楼层
weiryang 发表于 2015-7-14 03:06
点图为什么不刷新呢,偶用的火狐

你關閉到了 JS ?
或是沒有載入 JQ ?
V 端的區塊轉換有依賴於 JQ 的運作
发表于 2015-7-22 13:04:32 | 显示全部楼层

Message:  Call to undefined method Test::creat_captcha()

 楼主| 发表于 2015-7-23 10:03:44 | 显示全部楼层
yzhdaidai 发表于 2015-7-22 13:04
Message:  Call to undefined method Test::creat_captcha()

test 是指載入的 Model 名
你得看你放的 Model 是哪個名稱
发表于 2016-3-10 16:12:20 | 显示全部楼层
controller里的生成验证码方法岂不是直接可以在浏览器中访问啦?。。这样不友好吧

本版积分规则