Vidar Vestnes
Vidar Vestnes

Reputation: 42964

What is user_data in CodeIgniter session used to?

When i in CodeIgniter run var_dump($this->session->all_userdata()); it prints

array (size=5)
  'session_id' => string '3403084ad9f5e2582a8d9269ceb68fb7' (length=32)
  'ip_address' => string '127.0.0.1' (length=9)
  'user_agent' => string 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)' (length=64)
  'last_activity' => int 1365456079
  'user_data' => string '' (length=0)

Since these are automatically filled in values by CI, I'm wondering if I could/should overwrite the user_data index, with e.g 'user_data' => $username.

If CI keep update this index, this will not work. Than i must store my session-data with my own indexes. Anybody with experience on CI that knows what this fixed index in the session array is used for. There is no documentation for it.

Upvotes: 3

Views: 1898

Answers (1)

Filippo oretti
Filippo oretti

Reputation: 49817

the user_data field is used for all your extra session params

once you want to put a session value you use to do:

$this->session->set_userdata('x','y');

the userdata('x') is then stored in the user_data array key of the session with the y value

so your user_data key in session now will look like (or somenthing similar :D ):

'user_data' => ['x'=>'y']

so definitely ,yes , you WILL overwrite the value of user_data, just setting some session value you need/want

More About the user_data field from the doc seems to be the db field that CI uses to store your session params: http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

in the system7libraries/session.php there is no big trace of user_data just for these lines:

// Run the update query
        $this->CI->db->where('session_id', $this->userdata['session_id']);
        $this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));

which seems are updating a db field called user_data which (seems) to be related to the session db

Upvotes: 3

Related Questions