pctit 发表于 2011-6-29 20:52:38

看教程写了一个验证码页面!

<?php
class Show extends CI_Controller {
        function __construct() {
                parent :: __construct();
                header("Content-type: text/html; charset=utf-8");//设置页面为utf-8编码

                $this -> load -> database(); //载入数据库配置
                $this -> load -> helper('url'); //载入url辅助函数
                $this -> load -> helper('captcha');//载入验证码辅助函数
                $this -> load -> helper('form');//载入表单辅助函数
        }

        function index() {
                //设置验证码参数
                $vals = array(
                        'img_path' => './captcha/', //验证码图片保存路径
                        'img_url' => 'http://localhost/ci/captcha/', //验证码显示路径
                        'img_width' => '150',//验证码图片宽度
                        'expiration' => 120//验证码保存时长,超时自动销毁
                        );
                $cap = create_captcha($vals);

                $this -> db -> query("DELETE FROM ci_captcha WHEREip_address ='" . $this -> input -> ip_address() ."'"); //删除和本机IP相同验证码
                $data = array('captcha_time' => $cap['time'],
                        'ip_address' => $this -> input -> ip_address(),
                        'word' => $cap['word']
                        );
                $query = $this -> db -> insert_string('captcha', $data);
                $this -> db -> query($query); //验证码信息入库
                // 输出页面
                echo 'Submit the word you see below:';
                echo $cap['image'];//显示验证码
                //开始生成表单
                echo form_open('show/run');
                echo form_input('captcha', '');
                echo form_submit('mysubmit', '提交验证码!');
                echo form_close();
        }

        function run() {
                // 首先删除旧的验证码
                $expiration = time()-120; // 2分钟限制
                $this -> db -> query("DELETE FROM ci_captcha WHERE captcha_time < " . $expiration);
                // 然后再看是否有验证码存在
                $sql = "SELECT COUNT(*) AS count FROM ci_captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
                $binds = array($_POST['captcha'], $this -> input -> ip_address(), $expiration);
                $query = $this -> db -> query($sql, $binds);
                $row = $query -> row();

                if ($row -> count == 0) {
                        echo "验证码输入错误或验证码已过期!";
                        echo anchor('show/', '返回验证码页面', 'title="返回验证码页面!"');
                } else{
                        echo"验证码正确!";
                        echo anchor('show/', '返回验证码页面', 'title="返回验证码页面!"');
                }

        }
}

ci-ouu 发表于 2011-6-30 15:17:30

Good!

jayjun 发表于 2012-10-13 11:06:57

good!!!!

sinopf 发表于 2013-2-22 16:43:07

违背MVC了吧

0xz 发表于 2013-4-3 16:12:04

觉得CI手册介绍的把验证码存到数据库,很哆嗦,生个验证码也要搞一下数据库,个人觉得用缓存好一点

我是新手 发表于 2013-10-18 10:55:15

很好。如果200人同时访问。。。。。。。
页: [1]
查看完整版本: 看教程写了一个验证码页面!