Question

I am working on a login script and it used to work perfectly but then all of a sudden it stopped working. I commented out a lot of code but it still does the same. When I login it sees the userdata but it disappears right after.

This is what I got in my controller: (I leave commented code out)

Login controller:

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

Login Model:

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

I am using modular extensions. I am sure there is nothing wrong in My_Controller because I tested it by setting it back to MX_Controller. (I never changed anything in MX_Controller)

Was it helpful?

Solution

I found the answer to my question. In my model I was adding all data of the logged in user. One of the columns contain the IP Address. And I don't know why exactly but when I add the IP Address to the session it removes the session, and adds a new session with the default codeigniter session data and a new session id.

So somehow I can't store IP Addresses in my userdata.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top