ci驗證碼

來源:互聯網
上載者:User

標籤:style   blog   java   color   使用   檔案   

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="<?php echo site_url(‘yes/ajaxvalidate‘); ?>" /><input type="hidden" name="validate" id="validate" value="<?php echo $_SESSION[‘sess_validate‘];?>" /><input type="text" name="mycode" value="" /><br /><a id="ff"><?php echo $img[‘image‘]; ?></a><a id="clickme">點擊重新整理</a><br /><input type="submit" name="sub" value="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>以上只是說明方法,其實在一個頁面中,把上面的兩個方法融到別的控制器即可,這樣就可以運用了

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.