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

[已解决] 为什么我写得验证码类session总是慢一步?【求助管理员】

[复制链接]
发表于 2012-10-17 10:51:33 | 显示全部楼层 |阅读模式
本帖最后由 一迢 于 2012-10-19 19:37 编辑

写的验证码类如下,放入了libraryies中
PHP复制代码
 
    class Captcha{
        private $chars = '';
        private $code = '';
        private $img = null;
        private $font = null;
        private $fontsize = 13;
        private $fontcolor = null;
        private $ci = null;
        public $length = 4;
        public $width = 145;
        public $height = 20;
 
        public function __construct(){
            $this->chars = '0123456789';
            $this->font = FCPATH.'source/font/Consola.ttf';
            $this->ci = & get_instance();
        }
        /*随机生成验证码,并写入session*/
        private function createCode(){
            $length = strlen($this->chars) - 1;
            $code = '';
            for($i=0;$i<$this->length;$i++){
                $code .= $this->chars[mt_rand(0,$length)];
            }
            $this->code = $code;
 
            $this->ci->load->library('session');
            $this->ci->session->set_userdata(array('captcha' => $this->code));      
        }
        /*生成背景*/
        private function createBg(){
            $this->img = imagecreatefromjpeg(FCPATH.'/source/codebg/background'.mt_rand(1,8).'.jpg');
        }
        /*写入验证码*/
        private function writeCode(){
            $x = $this->width / $this->length;
            for($i = 0;$i < $this->length;$i++){
                $this->fongcolor = imagecolorallocate($this->img,mt_rand(0,55),mt_rand(0,55),mt_rand(0,55));
                imagettftext($this->img,$this->fontsize,0,$x*$i/4 + $this->width/2.6,16,$this->fongcolor,$this->font,$this->code[$i]);
                //imagestring($this->img,4,($x*$i/4 + $this->width/2.6),2, $this->code[$i], $this->fontcolor);
            }
        }
        /*生成验证码图片*/
        private function createCodeImg(){
            header("Pragma:no-cache");
            header("Cache-control:no-cache");
            header('Content-type:image/png');
            imagepng($this->img);
            imagedestroy($this->img);
        }
        /*前端显示验证码图片*/
        public function showcode(){
            $this->createCode();
            $this->createBg();
            $this->writeCode();
            $this->createCodeImg();
        }
    }
 
复制代码


调用:
PHP复制代码
 
    class test extends FR_Controller{
        private $temp = '';
 
        function __construct(){
            parent::__construct();
        }
 
        function index(){
            echo '<img src="'.site_url('test/captcha').'" alt="验证码" /><br/>';
            echo $this->session->userdata('captcha');
            //$this->captcha->showcaptcha();
        }
 
        function captcha(){
            $this->load->library('captcha');
            $this->captcha->showcode();
        }
 
        function date_time(){
            $this->load->helper(array('time'));
            $date = getWeek('2012-09-23');
            echo date('Y-m-d',$date['start']);
        }
    }
 
复制代码

但是我的session总是慢一步啊!一直没想明白……


 楼主| 发表于 2012-10-19 19:36:51 | 显示全部楼层
为什么还没有人回来回答我啊?
发表于 2012-10-23 01:03:17 | 显示全部楼层
echo '<img src="'.site_url('test/captcha').'" alt="验证码" /><br/>';
显示验证码的请求还没发起
echo $this->session->userdata('captcha');
当然不是最新的了
发表于 2012-10-23 14:31:53 | 显示全部楼层
楼上正解
发表于 2012-10-24 17:03:20 | 显示全部楼层
lamtin 发表于 2012-10-23 01:03
echo '';
显示验证码的请求还没发起
echo $this->session->userdata('captcha');

我也碰到这个问题,你说的原因我貌似懂了,但是怎么改啊?????急求,在线等,万分感谢!!!

本版积分规则