CodeIgniter Forums
Session proper config? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Session proper config? (/showthread.php?tid=64608)



Session proper config? - solidcodes - 03-11-2016

autoload.php
Code:
$autoload['libraries'] = array(
    'database', 'email', 'session'
);

config.php
Code:
$config['encryption_key'] = 'Xasdf32zsdfj0e98ehBDS3xS9j3DAIHKwdfweHfdksj34Ifreaky0120302weakyshitPSYCHOP4324342ath';
...
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = $_SERVER['DOCUMENT_ROOT'] .'/ci/ci3-codes-mastery2/session/';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

controller
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Test extends CI_Controller {

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

        //$this->load->library('session');
    }

    public function index()
    {
        $newdata = array(
                'username'  => 'johndoe',
                'email'     => '[email protected]',
                'logged_in' => TRUE
        );

        $this->session->set_userdata($newdata);
        
        $this->load->view('test');
    }
    
    
}

view
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

        $data = $this->session->newdata;  
        $data2 = $this->session->userdata('newdata');


        echo 'Session Test...';
        print_r($data2);
?>

But when I run these codes nothing, the session data is not displayed.
What am I missing here?

Thanks in advance.


RE: Session proper config? - keulu - 03-11-2016

it's because you don't affect key to your session array.

In your case, you can access to your vars like that.
PHP Code:
$username $this->session->username;
$email $this->session->email;
$logged_in $this->session->logged_in


if you want to get your complete array, set your session with a key
PHP Code:
$newdata = array(
        
'username'  => 'johndoe',
        
'email'     => '[email protected]',
        
'logged_in' => TRUE
);

$this->session->set_userdata('newdata'$newdata);
// OR
$this->session->set_userdata(array('newdata'=>$newdata)); 

you can check if a key is available with
PHP Code:
$this->session->has_userdata('newdata'); // (false in your case) 



RE: Session proper config? - salain - 03-11-2016

You only need to change how you retrieve the session data from:

PHP Code:
$data2 $this->session->userdata('newdata'); 
to:

PHP Code:
$data2 $this->session->userdata(); 
 

As per the manual http://www.codeigniter.com/user_guide/libraries/sessions.html#retrieving-session-data


RE: Session proper config? - solidcodes - 03-11-2016

(03-11-2016, 06:35 AM)keulu Wrote: it's because you don't affect key to your session array.

In your case, you can access to your vars like that.
PHP Code:
$username $this->session->username;
$email $this->session->email;
$logged_in $this->session->logged_in


if  you want to get your complete array, set your session with a key
PHP Code:
$newdata = array(
 
       'username'  => 'johndoe',
 
       'email'     => '[email protected]',
 
       'logged_in' => TRUE
);

$this->session->set_userdata('newdata'$newdata);
// OR
$this->session->set_userdata(array('newdata'=>$newdata)); 

you can check if a key is available with
PHP Code:
$this->session->has_userdata('newdata'); // (false in your case) 

thanks dude very helpful.

The document is very confusing.
https://codeigniter.com/user_guide/libraries/sessions.html#adding-session-data

Can someone please update the document.
Thanks in advanced.


RE: Session proper config? - InsiteFX - 03-30-2016

PHP Code:
$config['sess_save_path'] = APPPATH.'tmp'

$config['sess_save_path'] = FCPATH.'tmp' 

I use one of the above to store my session files, I prefer using the APPPATH one.