codeigniter会话不适用于同一控制器的其他方法

时间:2016-12-23 08:37:37

标签: php codeigniter session

我使用验证码制作登录控制器,并使用回调规则检查并验证验证码输入。

因此,我尝试使用会话将captcha变量发送到同一控制器中的另一个方法。

当我在另一个函数中回显会话时,它无效。它仅在我尝试在同一函数中回显时才有效。

以下是代码:

1。登录功能

function MyRtfData(objectID) {
    var i;
    var xmlDOM = new ActiveXObject("Microsoft.XMLDOM");
    xmlDOM.validateOnParse = false;
    xmlDOM.async = false;
    var node = xmlDOM.createProcessingInstruction("xml", "version='1.0' encoding='ISO-8859-1'");
    xmlDOM.appendChild(node);
    var xmlRoot = xmlDOM.createElement("EADATA");
    xmlDOM.appendChild(xmlRoot);
    var xmlDataSet = xmlDOM.createElement("Dataset_0");
    xmlRoot.appendChild(xmlDataSet);
    var xmlData = xmlDOM.createElement("Data");
    xmlDataSet.appendChild(xmlData);
    var xmlRow = xmlDOM.createElement("Row");
    xmlData.appendChild(xmlRow);
    var xmlName = xmlDOM.createElement("DateGen");
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth() + 1; //January is 0!
    var yyyy = today.getFullYear();
    if (dd < 10) {
        dd = '0' + dd
    }
    if (mm < 10) {
        mm = '0' + mm
    }
    today = mm + '/' + dd + '/' + yyyy;
    xmlName.text = today;
    xmlRow.appendChild(xmlName);
    var xmlName = xmlDOM.createElement("TimeGen");
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()
    if (minutes < 10) {
        minutes = "0" + minutes
    }
    xmlName.text = hours + ":" + minutes + " ";
    xmlRow.appendChild(xmlName);
    var xmlName = xmlDOM.createElement("Author");
    xmlName.text = Session.UserName;
    xmlRow.appendChild(xmlName);
    var xmlName = xmlDOM.createElement("EA_REPOS");
    xmlName.text = Repository.ConnectionString;
    xmlRow.appendChild(xmlName);
    return xmlDOM.xml;
};

2。检查capthca功能

public function login()
{
    if($this->auth->is_logged_in() === TRUE)
    {
        redirect(base_url()."dashboard");
    }

    $username = $this->input->post('username');
    $password = $this->input->post('password');

    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
    $this->form_validation->set_rules('captcha', 'Captcha', 'required|callback_check_captcha');

    if($this->form_validation->run() == FALSE)
    {
        $cap                = $this->created_captcha();
        $data['cap_img']    = $cap['image'];
        $data['title']      = 'Form Login Administrator';

        $this->session->set_userdata('captcha', $cap['word']);
        $sess = $this->session->userdata('captcha');

        echo ' <br/> FIRST SESSION : '.$sess . ' --- ';

        $this->load->view('form-login', $data);

    }
    else
    {
        $check_login = $this->auth->do_login($username,$password);

        if($check_login == FALSE)
        {
            $this->session->set_flashdata('msg', 'Your username or password wrong, please repeat again !!!');
            redirect(base_url().'login');
        }
        else
        {
            $lastlogin = $this->dbm->update('users', array('userid'=>$this->session->userdata('userid')), array('lastlogin'=>date('Y-m-d H:i:s')) );

            $this->session->unset_userdata('captcha');

            redirect(base_url().'dashboard');   

        }
    }
}

我试图将会话库放在构造函数中,但仍然是相同的。

public function check_captcha($input)
{
    echo '<br/> SECOND SESSION : '. $this->session->userdata('captcha') . ' --- ' .
         '<br/> POST INPUT : '.$input.' --- ';  

    if($input === $this->session->userdata('captcha')) 
    {

        return TRUE;
    }
    else
    {
        $this->form_validation->set_message('check_captcha', 'you input the wrong %s!'); 

        return FALSE;
    }
}

我在这里犯了错误吗?请帮助,谢谢。

修改: 这只发生在Chrome浏览器中。

1 个答案:

答案 0 :(得分:0)

您没有在check_captcha方法中获取验证码会话,因为您的验证码会话仅在表单未经验证时创建。

if($this->form_validation->run() == FALSE)..

之前添加以下代码行
    $cap                = $this->created_captcha();
    $data['cap_img']    = $cap['image'];
    $data['title']      = 'Form Login Administrator';

    $this->session->set_userdata('captcha', $cap['word']);