Codeigniter userdata在创建后立即删除

时间:2014-04-18 10:34:42

标签: php codeigniter session user-data

我正在处理一个登录脚本,它曾经很好地工作,但突然间它停止了工作。我评论了很多代码,但它仍然做同样的事情。当我登录时,它会看到用户数据,但它会立即消失。

这是我在我的控制器中得到的:(我留下注释代码)

登录控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends MY_Controller {

    public function __construct(){
        parent::__construct();
        $this->smarty->assign('header',"../../../views/login_header.tpl");
        $this->smarty->assign('footer',"../../../views/login_footer.tpl");
        $this->load->library('form_validation');
    }

    public function index()
    {
        $this->load->model('mdl_login');
        $email = $this->input->post('email');
        $password = $this->input->post('password');

        $this->form_validation->set_rules('email', 'Email',  'required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'required|callback_credential_check');


        if ($this->form_validation->run() == FALSE)
        {
            $errors = validation_errors();
            $this->smarty->assign('errors', $errors);
        }
        elseif($this->mdl_login->login($email, $password) == FALSE){
            $errors = 'Your login information is incorrect.';
            $this->smarty->assign('errors', $errors);
        }
        else //Validation success
        {

            //Get user
            $user = $this->mdl_login->user($email);
            $user['logged_in'] = TRUE;


            //It's not the if statement here because I also tried without it
            if($this->session->userdata('logged_in') == FALSE){
            $this->session->set_userdata($user);
            }

            print_r($this->session->all_userdata());
            die(); //At this point the userdata is correct but on refresh or going to another page it isn't

            //Redirect to homepage
            //redirect('dashboard', 'refresh');
        }
        $this->smarty->view('login/login.tpl');
    }
}

登录模式:

<?php
class Mdl_login extends CI_Model {
    public function login($email, $password){
            return TRUE;
    }

    public function user($email){
        $query = $this->db->get_where('korisnici_tbl', array('email' => $email));
        return $query->row_array();
    }
}

?>

My_Controller:

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/* load the MX_Controller class */
require APPPATH."third_party/MX/Controller.php";

class MY_Controller extends MX_Controller {
    public function __construct(){
        print_r($this->session->all_userdata());
        $this->smarty->assign('header',"../../../views/header.tpl");
        $this->smarty->assign('footer',"../../../views/footer.tpl");
        $this->smarty->assign('form_url_plugin',"../../../views/form_url_plugin.tpl");
        $this->smarty->assign('controller',$this->uri->segment(1));
        $this->smarty->assign('userdata',$this->session->all_userdata());

        if($this->session->userdata('logged_in') != TRUE && $this->uri->segment(1) != 'login'){
            if($this->session->userdata('logged_in') != TRUE){
                print_r($this->session->all_userdata());
        }
            //redirect('login', 'refresh');
        }
    }
}

我正在使用模块化扩展。我确信My_Controller没有任何问题,因为我通过将其设置回MX_Controller来测试它。 (我从未改变过MX_Controller中的任何内容)

1 个答案:

答案 0 :(得分:1)

我找到了问题的答案。在我的模型中,我添加了登录用户的所有数据。其中一列包含IP地址。我不知道为什么会这样,但是当我向会话添加IP地址时它会删除会话,并添加一个带有默认codeigniter会话数据和新会话ID的新会话。

所以我不能在我的用户数据中存储IP地址。