首页 > 代码库 > ci验证码

ci验证码

ci 之验证码为了方便,把 system/system/helpers/captcha_helper.php复制放在 application/helpers/文件夹里面手册上面推荐用数据库,但个人认为用 $_SESSION方便些(session必须开启)打开 application/helpers/captcha_helper.php文件,在判断 $word变量之后加上 $_SESSION[sess_validate] = $word;即在:$length    = strlen($word);之前加上$_SESSION[sess_validate] = $word;即可其实 $_SESSION[sess_validate]中的sess_validate随便自己命名,与别的不冲突就行了,新建一个控制器,如 yes.php<?php if ( ! defined(BASEPATH)) exit(No direct script access allowed);class Yes extends CI_Controller {            //根据手册设置验证码的参数,我是在源码里面设置为 4位验证码,且用的全是数字    public function __construct()    {        parent::__construct();        session_start();    }    // 初始化,即初次使用    function validatecode()    {        $this->load->helper(captcha);        $vals = array(            img_path => ./captcha/,            img_url => base_url() . captcha/,            img_width => 150,            img_height => 30,            expiration => 100    );    $cap[img] = create_captcha($vals);    $this->load->view(validate, $cap);        }    // 刷新验证码用    function ajaxvalidate()    {        $this->load->helper(captcha);        $vals = array(            img_path => ./captcha/,            img_url => base_url() . captcha/,            img_width => 150,            img_height => 30,            expiration => 100    );        $cap[img] = create_captcha($vals);        echo $_SESSION[sess_validate] . $cap[img][image];    }}?>展示页面(即模板文件((validate.php))可这样用(自己引入 jquery库),提交前可用 js 先检测一次是否输入正确的验证码<form action="" method="post"><input type="hidden" name="_url" id="vurl" value=http://www.mamicode.com/"<?php echo site_url(‘yes/ajaxvalidate‘); ?>" /><input type="hidden" name="validate" id="validate" value=http://www.mamicode.com/"<?php echo $_SESSION[‘sess_validate‘];?>" /><input type="text" name="mycode" value=http://www.mamicode.com/"" /><br /><a id="ff"><?php echo $img[image]; ?></a><a id="clickme">点击刷新</a><br /><input type="submit" name="sub" value=http://www.mamicode.com/"submit" /></form><script type="text/javascript">    $(document).ready(function(){        $("#clickme").click(function(){                                        var _url = $(#vurl).val();            $.ajax({                url:_url,                type:post,                async:false,                data:{name:11},                success: function(data)                {                    $(#ff).html(data.substr(4));                    $(#validate).val(data.substr(0,4));                }            });                            });    });</script>以上只是说明方法,其实在一个页面中,把上面的两个方法融到别的控制器即可,这样就可以运用了