I have a problem about session in CodeIgniter. I initialize a session in Login/submit Then , i have to check in another controller if the user is logged. So i would use:
if ( $this->session->userdata('login_state') == FALSE ) {
$this->load->view('header'); // no session established, kick back to login page
}
The problem is that in the controller in which i would check the login_state , i can't use the $this->session
variable.
If i load the session library in this controller , i'll initialize a new session and the $this->session->userdata('login_state')
will be always false.
I tried the :
$ci = & get_instance();
$ci->session->userdata('login_state');
but it doesn't work.I don't know why.. I would not create a class to share the session variable. is there a clear way to pass the session variable through controllers?
Login controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function submit() {
$this->load->library('session');
// Prendo i dati passati dal form
$username = $this->input->post('username');
$password = $this->input->post('password');
// read user's credentials from db, through Login Model
if ( $username == "my_username" && $password == "my_password" ) {
$this->session->set_userdata('login_state', TRUE);
}
else {
redirect( '/' ); // redirect back to login page
}
}
ragazze controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Ragazze extends CI_Controller {
public function search()
{
if ( $this->session->userdata('login_state') == FALSE ) {
$this->load->view('header'); // no session established, kick back to login page
}
else{
$this->load->model('ragazza_model');
#Prelevo il valore passato tramite post
$nome_ragazza=$this->input->post('nome');
if ($nome_ragazza) {
$nome_ragazza = $this->ragazza_model->get_girl_by_name($nome_ragazza);
}
$data['nome_ragazza'] =$nome_ragazza;
$this->load->helper('form');
$this->load->model('ragazza_model');
$this->load->helper('url');
$this->load->library('form_validation');
$this->form_validation->set_rules('nome', 'Nome', 'required');
if ($this->form_validation->run() == FALSE){
//Questo viene eseguito anche quando il form
//non `e compilato, cio´e la prima volta che si -
//la pagina con il form html
$this->load->view('header');
$this->load->view('menu');
$this->load->view('ragazze/search',$data);
$this->load->view('footer');
}
else{
//Se i controlli sono positivi, redireziono l' -
$this->load->view('header');
$this->load->view('menu');
$this->load->view('ragazze/search',$data);
$this->load->view('footer');
}
}
}
config.php
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
EDIT: ok i discovered somethin interesting. If i use Explorer..everythings works..So the problem is Firefox..i don't know Why. Can you help?
EDIT: I tried a lot of things.. but at the end mybe it was a cookie problem of my firefox. I deleted oll my cookies and now the session seems to work. Really , i don't know if the problem was this. I changed also somethings in the config but before to delete my cookies it didin't work. So guys , if you have my same problem with Firefox ( cause in Explorer the session, so the same code, worked) try just to delete your cookies. If someone has an answer to this strange thing, i'll appreciate it.