• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with SESSION.

#1
[eluser]Yakow[/eluser]
Hello dudes!
Im new in CI, so i have a basic question and need help.

is the basic login system, so, in my home.php i put this code:

Code:
function login(){
  $this->load->library('form_validation');
  $this->load->library('session');  
  $config = array(
                array(
                      'field'=>'login',
                      'label'=>'Login',
                      'rules'=>'required|min_length[4]|max_length[10]| htmlspecialchars'
                ),
     array(
                      'field'   => 'senha',
                      'label'   => 'Senha',
                      'rules'   => 'required| min_length[4]| max_length[10]| htmlspecialchars'
                )
              );
        $this->form_validation->set_rules($config);
  
  if($this->form_validation->run() == FALSE){
   $this->index();
  }
  else{
  
   $data['login'] = $this->input->post('login');
   $data['senha'] = $this->input->post('senha');    
  
   $this->load->model('administracao/usuarios_model');
   $login = $this->usuarios_model->login($data);
  
   if(count($login)>0){
    
    $newdata = array(
     'nome_usuario' => $login[0]->nome,
                    'usuario'  => $data['login'],
                    'loggedin' => TRUE
    );
    
    
    $this->load->library('session');
    $this->session->set_userdata($newdata);
    print_r($this->session->all_userdata());  
    redirect('administracao/produtos');
   }
   else{
    $this->index();
   }
  }

When i print "print_r($this->session->all_userdata());" i can recive $newdata...

So when i go another controller "produtos", i have this code:

Code:
function __construct(){
  parent::__construct();
  $this->load->library('session');
  $this->load->helper('conversor_de_formatos');
  
  print_r($this->session->all_userdata());  
  
        if(!$this->session->set_userdata('loggedin')){
   echo "Error.";
   exit;
        }
  
  
}

So, when i print print_r($this->session->all_userdata()); i can't get the information came from $newdata in home controller, and i can't get the view.

Why ??

Sorr for my english.

#2
[eluser]Cristian Gilè[/eluser]
If you want to check if the user is logged in, you have to use userdata not set_userdata:

Code:
if(!$this->session->userdata('loggedin'))
{
    $this->session->set_flashdata('message', 'You must login');
    redirect('path/to/login/controller');
}

#3
[eluser]Yakow[/eluser]
Cristian now i put userdata('...

and when i do print_r, return me this:
Code:
Array ( [session_id] => 4368e98a1a2811b9cdc627ed70613673 [ip_address] => 127.0.0.1 [user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1 [last_activity] => 1338828074 [user_data] => )

look the empty user_data

#4
[eluser]Cristian Gilè[/eluser]
Post the session settings of the config file.

#5
[eluser]Yakow[/eluser]
i put the default config, now its working Smile
So ty so much and sorry for my newbie question!!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.