Welcome Guest, Not a member yet? Register   Sign In
Session proper config?
#1

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.
No SEO spam
Reply
#2

(This post was last modified: 03-11-2016, 06:38 AM by keulu.)

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) 
Reply
#3

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/li...ssion-data
Reply
#4

(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/libra...ssion-data

Can someone please update the document.
Thanks in advanced.
No SEO spam
Reply
#5

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB