Codeigniter Session - Access Userdata value for al

2019-06-14 04:08发布

问题:

I am using Codeigniter's Session class to store session values in the database. Contained within the userdata field is the user's username and other values.

I want to get the username from each userdata field where the lastactivity is >= let's say 5 minutes.

The only way I can think to accomplish this is:

  • search for users active in last 5 minutes
  • get the userdata for each row returned
  • unserialise userdata
  • extract username

However this seems like the long way. Is there a cleaner/better way to do this?

回答1:

Try this code.

$query = $this->db->select('user_data')->get('ci_sessions');

$user = array(); /* array to store the user data we fetch */

foreach ($query->result() as $row)
{
    $udata = unserialize($row->user_data);

    /* put data in array using username as key */
    $user[$udata['user_name']] = $udata['user_role']; 
}

from the result array, u can fetch needed information.