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

[已解决] CI验证码如何使用。

  [复制链接]
发表于 2011-5-5 09:36:00 | 显示全部楼层 |阅读模式
不废话,上手册源码。我的是2.0.2最新版本的CI.
   $this->load->helper('captcha');//这个在_construct里加和在function index()里加一样没什么反应。
$vals = array(
     'word' => 'Random word',//这个改成随机数,也没什么反应。
     'img_path' => './captcha/',//这个目录已经建了,而且里面放了图片。
     'img_url' => 'http://localhost/ci/captcha/',//这个调成了我的路径。
     'font_path' => './path/to/fonts/texb.ttf',//这个不懂,字体?
     'img_width' => '150',//图片宽
     'img_height' => 30,//图片高。为啥宽有引号,高没有,改成一致的就出错。
     'expiration' => 7200//删掉垃圾时间。
     );
  $cap = create_captcha($vals);//把验证码造出来。
  echo $cap['image'];//输出。


输出为空。如果放在$data[images] = $cap['image'];里面,前台拿不到。报错。

回想也是啊,你$cap['image'];里面的image从哪里冒出来的啊。
高手给你CI验证码的案例吧,求救贴那么多,都没人解决。
 楼主| 发表于 2011-5-5 16:13:38 | 显示全部楼层
没人理我哟,太菜了哟,没人搭理哟,。
这问题难死我了,这问题不值一答哟,气死我了哟。
发表于 2011-5-5 16:56:31 | 显示全部楼层
我用你的方法验证了下 没问题啊!
 楼主| 发表于 2011-5-5 16:58:06 | 显示全部楼层
我又建了一个方法。
         function aa(){
                 $vals = array(
    'word' => '123',
    'img_path' => './style/',
    'img_url' => 'http://localhost/ci/style/',
    'font_path' => './path/to/fonts/texb.ttf',
    'img_width' => '150',
    'img_height' => 30,
    'expiration' => 120
    );
然后http://localhost/ci/index.php/admin/aa终于能看到CI验证码了,但是前台调用正在研究。
<img src=???????????????????????/>
 楼主| 发表于 2011-5-5 17:03:28 | 显示全部楼层
回复 3# mageguoshi


    前台<img src后的地址怎么写啊,还有后台怎么验证用户输入验证码的对错啊。我看CI做者写的原始码了,好像没开session啊/
 楼主| 发表于 2011-5-5 17:10:42 | 显示全部楼层
http://localhost/ci/index.php/admin/aa这个地址可以看到验证码。
http://localhost/ci/index.php/admin这个页面里面<img src="<?php echo site_url('admin/aa');?>" width="140" height="50" />调不到验证码。并且其他格式失效。
 楼主| 发表于 2011-5-5 21:43:10 | 显示全部楼层
研究终于有了小小的结果。本社区的宗旨是自己动手,丰衣足食。能帮你的只有自己了。我发的帖子都是自问自答。感觉自己很有意思。就算是我学CI的笔记吧,上源码。
先引入$this->load->helper('captcha');
英文手册内容。$vals = array(
    'img_path' => './captch/',
    'img_url' => 'http://localhost/ci/captch/',
    'font_path' => './path/to/fonts/texb.ttf',
    'img_width' => '120',
    'img_height' => 25,
    'expiration' => 120
    );

                $cap = create_captcha($vals);
        echo $cap['image'];//现在访问可以看到验证码,不过宽超过120时,验证码有时显示不全。
                $data['img']=$cap['image'];
                $this->load->view('admin/admin',$data);//装到data里面,前台这样调用。
echo $img;//能直接输出验证码。
请看作者写的system/helper下的captcha_helper.php文件,它返回三个值,return array('word' => $word, 'time' => $now, 'image' => $img);
那个word就是随机生成的字码,判断用户提交的和那个word相同即可。
   echo  $cap['word'];//拿到word
 楼主| 发表于 2011-5-5 21:48:45 | 显示全部楼层
CI的MVC模式笔记。
C层:         $this->load->model('Mhome');
        $data['dbcon'] = $this->Mhome->get_db();
        $data['page_title']= 'CI开发新闻系统';
        $this->load->view('head',$data);
          $this->load->view('index');
          $this->load->view('tail');

M层:        class Mhome extends CI_Model {
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
                function get_db(){
                        $query=$this->db->query('select * from category');
                        return $query->result();
                }
                function get_article($category_id){
                        $query=$this->db->query("select * from article where category_id=$category_id");
                        return $query->result();
                }
        }

V层:拿data里的值<?php echo $page_title;?>
CI所谓干净的url<base href="<?php echo base_url();?>"/>
循环,其实是PHP原生代码。      <?php foreach ($dbcon as $row):?>
      <div class="tow"><?php echo anchor('home/content/'.$row->category_id,$row->category_name)?></div>
      <?php endforeach; ?>
 楼主| 发表于 2011-5-5 21:53:02 | 显示全部楼层
CI验证码:
源码:        $vals = array(
    'img_path' => './captch/',
    'img_url' => 'http://localhost/ci/captch/',
    'font_path' => './path/to/fonts/texb.ttf',
    'img_width' => '120',
    'img_height' => 25,
    'expiration' => 120
    );

                $cap = create_captcha($vals);
        echo $cap['image'];

                $data['img']=$cap['image'];
                $this->load->view('admin/admin',$data);
//注意引入函数。
页面获取:
echo $img;

作者写的函数返回三个值,echo $cap['word'];这个是产生的随机数,判断其余与客户提交的值相等即可。
 楼主| 发表于 2011-5-6 08:45:18 | 显示全部楼层
现在整理一下CI自带验证码的使用方法,版本:2.0.2
首先引入CI作者写的验证码方法。$this->load->helper('captcha');
初始化方法。
$vals = array(
    'word' => 'Random word',//指定随机数,可以不写,默认是数字和字母
    'img_path' => './captcha/',//路径,必须存在
    'img_url' => 'http://example.com/captcha/',//同上
    'font_path' => './path/to/fonts/texb.ttf',
    'img_width' => '150',//最好不要小于120,否则生成的验证码会超出范围
    'img_height' => 30,//验证码图片的高
    'expiration' => 7200//清除垃圾时间,可改为120即两分钟。
    );

$cap = create_captcha($vals);
echo $cap['image'];
访问这个方法就能看到验证码。
此方法在system/helpers下的captcha_helper.php里面。
看后知道,次方法返回三个值,其中$cap['word'];就是验证码的值,比较用户输入的值和word即可。

本版积分规则