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

[HELP] 关于$this? CI session 写入问题

[复制链接]
发表于 2011-3-23 16:49:02 | 显示全部楼层 |阅读模式
1.在libraries中有个验证码类verify 里面的一个方法 public function code(){return $this->vCode}//返回验证码
在控制器common中 输出验证码:
function verify() {
        $this->load->library("Verify");  //加载验证码类
        $verify = new Verify();  //创建对象
        $verify->setCheckImageWH("60","20");  //设置生成的验证码图片大小,145为宽度,20为高度
        $verify->outCheckImage();//输出图片
        $this->session->set_userdata('code',$verify->code());//问题:$verify->code()为空
    }
现在我只能在verify类中 输出图片的时候 写入session
请问这个原因是?
2.在APMs环境下,数据库带数字 如ci8cms 无法指定数据库。。。。  但cicms又可以,请问跟ci有关系吗?
3.在CI中,总觉得类名和文件名的统一性很模糊,控制控制器加前缀时, 文件名又不需带前缀?
发表于 2011-3-26 23:57:09 | 显示全部楼层
Verify 不是 CI 自带的。CI 没有 Verify 这个类库。
 楼主| 发表于 2011-3-28 14:37:09 | 显示全部楼层
回复 2# Hex


    Verify是自定义类 放在library
发表于 2011-3-28 14:40:29 | 显示全部楼层
verify 类库有问题吧。贴一下代码。
 楼主| 发表于 2011-3-28 14:50:58 | 显示全部楼层
回复 4# Hex


    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* 驗證碼生成
*author:haile
*/

class Verify {
//验证码位数
    private $vCheckCodeNum   = 4;

//产生的验证码
    private $vCheckCode    = '';

//验证码的图片
    private $vCheckImage   = '';

//干扰像素
    private $vDisturbColor   = '';

//验证码的图片宽度
    private $vCheckImageWidth = '80';

//验证码的图片宽度
    private $vCheckImageHeight   = '20';

    /**
     *
     * @brief   产生验证码
     *
     */
    private function createCheckCode() {
        $this->vCheckCode = strtolower(substr(md5(rand()),0,$this->vCheckCodeNum));
        return $this->vCheckCode;
    }

    /**
     *
     * @brief   产生验证码图片
     *
     */
    private function createVerifyImage() {
        $this->vCheckImage = @imagecreate ($this->vCheckImageWidth,$this->vCheckImageHeight);
        imagecolorallocate ($this->vCheckImage, 255, 255, 255);
        return $this->vCheckImage;
    }

    /**
     *
     * @brief   设置图片的干扰像素
     *
     */
    private function setDisturbColor() {
        for ($i=0;$i<=128;$i++) {
            $this->vDisturbColor = imagecolorallocate ($this->vCheckImage,255, 255, 255);
            imagesetpixel($this->vCheckImage,rand(2,128),rand(2,38),$this->vDisturbColor);
           
        }
    }

    /**
     *
     * @brief   设置验证码图片的大小
     *
     * @param   $width   宽
     *
     * @param   $height 高
     *
     */

    public function setCheckImageWH($width,$height) {
        if($width==''||$height=='')return false;
        $this->vCheckImageWidth   = $width;
        $this->vCheckImageHeight = $height;
        return true;
    }

    /**
     *
     * @brief   在验证码图片上逐个画上验证码
     *
     */
    private function writeCheckCodeToImage() {
        for ($i=0;$i<$this->vCheckCodeNum;$i++) {
            $bg_color = imagecolorallocate ($this->vCheckImage, rand(0,255), rand(0,128), rand(0,255));
            $x = floor($this->vCheckImageWidth/$this->vCheckCodeNum)*$i+3;
            $y = rand(0,$this->vCheckImageHeight-15);
            imagechar ($this->vCheckImage, 5, $x, $y, $this->vCheckCode[$i], $bg_color);
        }
    }
    /**
     *
     * @brief   输出验证码图片
     *
     */
    public function outCheckImage() {
        
        $this ->createCheckCode();
        $this ->createVerifyImage();
        $this ->setDisturbColor();
        $this ->writeCheckCodeToImage();
        $this ->writeCheckCodeToSession();
        header('Content-Type:image/jpeg');
        imagejpeg($this->vCheckImage);
    }

    public function writeCheckCodeToSession()
    {
        $ci =& get_instance();
        $ci->session->set_userdata('verifycode',  $this->vCheckCode);//本想在控制器才写入session 但返回不了code 只好在这里 在输出图片之前写入session
    }
    public function code()
    {
       return $this->vCheckCode;//这个原是想 在控制器应用到session的 但没有效果  控制器里面得到是null,我只好增加 function writeCheckCodeToSession 在这个类里面
    }
}
?>

本版积分规则