CodeIgniter Forums
simple problem with session (login) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: simple problem with session (login) (/thread-65619.html)



simple problem with session (login) - Sarog - 07-02-2016

Good day.
I broke my brain with this problem.

This is code in controller Login

PHP Code:
    private $logged_in;
    private 
$login;

    public function 
__construct()
    {
        
parent::__construct();
        
$this->load->model('login_model');

        if(
$this->session->userdata('logged_in')){
            
$this->logged_in true;
            
$this->login $this->session->userdata('login');
        }
        else{
            
$this->logged_in false;
            
$this->login '';
        }
    }

    public function 
index()
    {
        
$data_r=array();

        
$CI = & get_instance ();
        
$CI->load->view('site_head',$data_r);
        
$CI->load->view('site_reg',array('logged_in'=>$this->logged_in,'login'=>$this->login));
        
$CI->load->view('site_header');

        if(
$this->logged_in){
            
redirect('/''location');
        }
        else{
            
$CI->load->view('login_site',array('logged_in'=>$this->logged_in));
        }
        
$CI->load->view('site_footer');
    } 
After autorization if i put login button, i see that $this->logged_in is TRUE. ANd redirect to main page.
But this code main controller
PHP Code:
    private $logged_in;
    private 
$login;

    public function 
__construct()
    {
        
parent::__construct();

        
//$this->load->library('session');
        
if($this->session->userdata('logged_in')){
            
$this->logged_in true;
        }
        else{
            
$this->logged_in false;
        }
    } 

And in function index() $this->logged_in is FALSE

Why? Thanks for help


RE: simple problem with session (login) - arma7x - 07-02-2016

What is $this->session->userdata('logged_in') value? Have your dump $_SESSION? You can set session userdata by using $this->session->set_userdata('some_name', 'some_value'). The userdata() method returns NULL if the item you are trying to access does not exist.