Reputation: 1970
I have a CodeIgniter application still in development but I realise that when I log in and set user data in session, it gets set but disappears after redirecting. Therefore, I cannot access the session data after redirecting.
This is my login script
public function login(){
if(!empty($_SESSION['user_id'])){//Meaning you are logged in
//We inform you
$this->session->set_flashdata('msg', "<div class='alert alert-success'><span class=''></span> You are already logged in as <strong>".$this->session->user_name."</strong></div>");
//And send you back to your dashboard
return redirect('/account/dashboard');
}
if($this->input->post()){
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','required|valid_email');
$this->form_validation->set_rules('password','Password','required');
if($this->form_validation->run() == TRUE){
$creds=['email'=>$this->input->post('email'),'password'=>$this->input->post('password')];
$this->load->model('user/user_model');
$user=$this->user_model->login($creds);
if(!$user){
$data['error_msg']="Inavlid login details. Please retry";
}else{
$user->user_type=(!empty($user->user_type))?:'2';
$userdata=['user_id'=>$user->id,'user_name'=>$user->name,'user_email'=>$user->email,'user_type'=>$user->user_type];
$this->session->set_userdata($userdata);
$this->session->set_flashdata('msg', '<div class="alert alert-success"><span class="fa fa-check"></span> Logged in as '.$this->session->userdata('user_name').'</div>');
return redirect('account/dashboard');
}
}
}
$data['title']="Login";
$this->load->view('template/auth/header');
$this->load->view('account/login',$data);
$this->load->view('template/auth/footer');
}
and this is my config/congig.php file
$config['base_url'] = 'http://localhost/tmpad/';
$config['allow_get_array'] = TRUE;
$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
$config['directory_trigger'] = 'd';
$config['encryption_key'] = '4%^&*9799809-nkhdfioup';
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
$config['cookie_prefix'] = '';
$config['cookie_domain'] = 'http://localhost/tmpad/';
$config['cookie_path'] = '/';
$config['cookie_secure'] = FALSE;
$config['cookie_httponly'] = FALSE;
$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = TRUE;
$config['csrf_exclude_uris'] = array();
session is auto-loaded in config/autoload.php
I will appreciate any guidance or assistance. Thank you
Upvotes: 3
Views: 13490
Reputation: 276
If anyone gets here still looking for the solution, start by updating your Codeigniter libraries; the entire system folder to a later version like 3.1.5 (as at time of writing).
Upvotes: 0
Reputation: 243
Not sure if people are still being faced with this issue, But my solution is to check the permissions to the sessions folder:
application/cache/sessions
making the owner www-data in my case solves the problem:
sudo chown -R www-data:www-data ./application/cache/sessions/
Upvotes: 0
Reputation: 2145
I'm not sure what exactly is the problem. Recently I faced this too..
It was working before in my development running php7.0.
Currently it is only working in my production server running nginx and php 5.6. My development server seems to be not working and keeps on regenerate new row in sessions table. My development server is using php7.1, on homestead virtualbox development environment, usually being used for Laravel projects.
I managed to get over this by taking this step.
1) Go to system/libraries/Session/Session.php
2) Comment session_start() by adding //. We want to relocate the sessionn_start().
3) Go down to line 315 where it says Security is king, and comment out until line 351
4) Then go to your main index.php ( the root index.php )
5) Add session_start() at the top once.
6) Okay try again. Hopefully it works. My guess is that it is not working with php 7.1 and some update need to be done in this Session.php file.
My CI Version is 3.1.1
Upvotes: 15
Reputation: 82
Your code looks fine, if you comment the redirect line and create another action to dump session (for example dumpsession), then if you login and finally point your browser to BASEURL/controller/dumpsession can you see all the session data?
Probably the problem is not the redirect.
Upvotes: 0