繁体   English   中英

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

[英]codeigniter session is not working on other methods of the same controller

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

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

当我在另一个函数中回显会话时,它不起作用。 仅当我尝试在同一函数中回显它时,它才起作用。

这是代码:

1.登录功能

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');   

        }
    }
}

2.检查capthca功能

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;
    }
}

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

function __construct()
{
    parent::__construct();
    $this->load->library('session');
    $this->userid = $this->session->userdata('userid');
    $this->load->helper('captcha');
}

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

编辑:仅在Chrome浏览器中会发生这种情况。

您不会在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']);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM