Welcome Guest, Not a member yet? Register   Sign In
ci sessions user_data disappears
#1

[eluser]Miky llanderal[/eluser]
hello i have exactly the same issue than this guy problem codeigniter sessions

the problem is i logged id succesfully but switching between controller functions and views the data of the session disappears

this is my code inicio controller
Code:
<?php

class Inicio extends Controller {

    function  __construct() {
        parent::Controller();
    }

    function index($msg='') {
        $data['msg']=$msg;
        $lastExitos = new Caso_exito_model();
        $lastExitos = $lastExitos->getExitosResumidos();
        $lastNoticias = new Ultima_noticia_model();
        $lastNoticias = $lastNoticias->getUltimasNoticias();

        $data = array(
            'lastExitos' => $lastExitos,
            'lastNoticias' => $lastNoticias,
            'msg'=>$msg
        );

        $this->load->view('inicio_view', $data);
    }
    function virtual(){
         $data['head']="consultoria/header";
         $data['foot']="footera";
         $data['panel_left']="consultoria/panel_left_view";
         if($this->session->userdata('logged_in') === TRUE){
            $data['content'] = "consultoria/container";
            $data['autodiagnostico']="http://www.contactopyme.gob.mx/autodiagnostico/cgis/index.asp";
            $this->load->view('general_inst', $data);
         }
         else{
             redirect('/inicio/registro/', 'refresh');
         }
    }
    function login(){
        $user=$this->input->post('username');
        $pass=$this->input->post('password');
          
            $this->load->model('usuario_model');
        $log=$this->usuario_model->autenticar($user, md5($pass));
        if($log!=false){
          foreach($log->all as $row){
        $user_data=md5($row->id."&".$row->email);
              
          }
          $data = array(
                  'user_data'  => $user_data,
          'logged_in'  => TRUE
          );
          $this->session->set_userdata($data);
          //Actualizar session table
          $session_id = $this->session->userdata('id');
              
          $upd_session=$this->session_model->loggedin($data['user_data'],$session_id);
          if($upd_session=="done"){
        $this->acceso();
          } else {
        $this->index('<div id="message">El nombre de usuario y/o contraseña es incorrecto, intente de nuevo.</div>');
          }
        } else {
          $this->index('<div id="message">El nombre de usuario y/o contraseña es incorrecto, intente de nuevo.</div>');
        }
    }
        function logout()
    {
      $this->session->sess_destroy();
          $this->index('<div id="message">La sesión se ha cerrado exitosamente.</div>');
    }


    function acceso() {
if($this->session->userdata('logged_in') === TRUE){
                       // redirect('/oportunidades');

                        }
    }//Final

I have autoloaded session library, and i have this configuration:
Code:
$config['sess_cookie_name']        = 'key';
$config['sess_expiration']        = 0;
$config['sess_encrypt_cookie']    = TRUE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']        = 'session';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent']    = FALSE;
$config['sess_time_to_update']     = 7200;

anyone knows why is it happening?
#2

[eluser]WanWizard[/eluser]
Which browser? What is your cookie configuration?

Have you checked your session table? Is the initial session record created? Do you get a second record when you request another page? Did the browser receive and store the session cookie? Is the time on your server and client correct?
#3

[eluser]Miky llanderal[/eluser]
I'm using chrome and firefox in ubuntu 10/apache2 mysql etc.
the cookie conf I've tried putting the cookie domain localhost because i'm developing in local enviroment but i dont find difference with this
Code:
$config['cookie_prefix']    = "";
$config['cookie_domain']    = "";
$config['cookie_path']        = "/";



yes I checked the session table

Code:
CREATE  TABLE IF NOT EXISTS `vinculacion_new`.`session` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `session_id` VARCHAR(40) NULL ,
  `ip_address` VARCHAR(16) NULL ,
  `user_agent` VARCHAR(50) NULL ,
  `last_activity` VARCHAR(10) NULL ,
  `user_data` TEXT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = MyISAM,
COMMENT = 'almacenar sesiones\n' ;

charset =UTF-8 Unicode
collation utf8_general_ci


and yes the initial session record is created correctly with all the data, and when i request other page or controller or something the session changes and user_data get lost I'll show you the inserts statements:
Code:
INSERT INTO `session` (`id`, `ip_address`, `user_agent`, `last_activity`, `user_data`) VALUES
('82dadaf90c8ac80a8df0973a43a5b325', '0.0.0.0', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.24 (', 2011, '61f078c68b3595ff764a665612ac26d7');
and when i switch to other function/controller/page
Code:
INSERT INTO `session` (`id`, `ip_address`, `user_agent`, `last_activity`, `user_data`) VALUES
('fb2c8b165c1e7bad8bd81119fd690d39', '0.0.0.0', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.24 (', 1304496144, '');

And yes I deleted the cookies for testing and yes my browsers receive and store the cookie
the hour is ok.

I've tried to find the error but i can't find it
#4

[eluser]Miky llanderal[/eluser]
and sorry for post the same issue twice!
#5

[eluser]Miky llanderal[/eluser]
Other thing that I found is when I'm using my controller after be logged
Code:
$this->function();
to call another function, the sessions works well, including iterative. For example
Code:
$this-> function1();  
function1(){
if($this->session->userdata('loggedin') == TRUE){
$this-> function2();
}
function2(){
if($this->session->userdata('loggedin') == TRUE){
$this->function3();
}
}
}
!

But if I'm in a view and I click and anchor link the session get lost....
#6

[eluser]WanWizard[/eluser]
Your user_agent field is to small. CI 2.0.2 needs 120 characters. Your UA check probably fails after which the session is destroyed.

And note that you can not use 'localhost' for the cookie_domain, it's not accepted by Chrome. Leaving it empty isn't a goiod idea either. Better extract the hostname from the URI and use that, of configure it manually (for localhost, use 127.0.0.1).
#7

[eluser]Miky llanderal[/eluser]
I change the table field 'user_agent' to 120 caracters and I put cookie domain 127.0.0.1, but it still be the same, in other hand, I put the project in other server, no in localhost enviroment and the sessions including my small field work well :S, which would be the issue?
#8

[eluser]Miky llanderal[/eluser]
i read more about this and i put http://127.0.0.1/ in cookie domain but still wrong, i think that my problem may be the cookie, maybe the session doesn't find the cookie and that is why it starts other session .... but i don know how to fix this, i dont know how to set a cookie correctly in localhost enviroment,
#9

[eluser]Miky llanderal[/eluser]
Update! I've tracked the error and I found the cause, I was setting the last activity in the table sessions with a function date() not with strtime(now) for timestamp, I changed it and everything works well
Thank's for your help, it really helped me
#10

[eluser]WanWizard[/eluser]
What do you mean "I was setting"?

last_activity is an internal variable, you don't have to (and should not) touch that.




Theme © iAndrew 2016 - Forum software by © MyBB