Welcome Guest, Not a member yet? Register   Sign In
how many are logged in?
#1

(This post was last modified: 11-06-2015, 08:59 AM by jfi.)

Hello,

I have successfully made login with sessions with codeigniter 3 with database driver.

I have sessions strored in ci_sessions table. User data is in the last blob called data.

ci-sessions-table structure:
1 id varchar(40)
2 ip_address varchar(45)
3 timestamp int(10)
4 data blob

I'm trying to make a simple notice to logout page that shows how many users are still logged in after you have gone. Just to have practise with playing with the ci_session table user data. Now the number of sessions are shown in the logout-page.

view-file: login.php 
 if (isset($stillOnline)) {  echo "<div> Still online: ".$stillOnline." users. </div>";  } 

user-controller: 
logout function:
public function logout() 
   {
   $array_items = array('username', 'email', 'logged_in');
   $this->session->unset_userdata($array_items);
   $query = $this->db->query('SELECT * FROM ci_sessions');
   foreach ($query->result() as $row) {
          //$user_data = $row->data;
          //$online=$user_data['logged_in'];
          //$online=$row->userdata('logged_in');
          // if ($online) $i++;
          }
   $data['stillOnline'] = $query->num_rows();
   $this->load->view('login', $data);
   }

What should I put into foreach-loop to count down the in-logged users by analyzing the ci-session table?


Thanks.
Jouni
ps. Below other code...

Autoland:
$autoload['helper'] = array('array','html','email','url', 'file','form');
$autoload['drivers'] = array('session');
$autoload['libraries'] = array('database','session','xmlrpc','form_validation');


user-controller:
part of login function:
....
$newdata = array(
        'username'  => $username,
        'email'     => '[email protected]',
        'logged_in' => TRUE
        );
$this->session->set_userdata($newdata);
  $this->load->view('formsuccess'); 
Reply
#2

Assuming that you're destroying the user's session when the user logs out, you would just select the count of all sessions with
Code:
'timestamp > ' . (time() - $maxlifetime)
(to borrow from the session database driver's gc() method, remembering to use greater than instead of less than, since we want the newest sessions, not the oldest sessions).

Just set $maxlifetime in this case to whatever you think is a valid timeframe in which you would consider a user still logged in, even though they haven't written new session data. Then you can use the string as the where clause in your query to retrieve the number of active sessions.
Reply
#3

(11-06-2015, 10:02 AM)mwhitney Wrote: Assuming that you're destroying the user's session when the user logs out, you would just select the count of all sessions with
Code:
'timestamp > ' . (time() - $maxlifetime)
(to borrow from the session database driver's gc() method, remembering to use greater than instead of less than, since we want the newest sessions, not the oldest sessions).

Just set $maxlifetime in this case to whatever you think is a valid timeframe in which you would consider a user still logged in, even though they haven't written new session data. Then you can use the string as the where clause in your query to retrieve the number of active sessions.

Thank you, mwhitney. That's a good solution. But generally I am just seeking the way to analyze blopped data that is quered to foreach loop...
Reply
#4

jfi Wrote:
PHP Code:
$newdata = array(
        
'username'  => $username,
        
'email'     => '[email protected]',
        
'logged_in' => TRUE
        
);
$this->session->set_userdata($newdata); 

Generally, the $newdata['logged_in'] value here should be useless, or avoided, because you should destroy the session when the user logs out, and you can't verify the email/username if the user isn't logged in.

(11-06-2015, 10:24 AM)jfi Wrote: Thank you, mwhitney. That's a good solution. But generally I am just seeking the way to analyze blopped data that is quered to foreach loop...

Session data is intended to be used per user and per application, so it is not necessarily available to other users or other applications. You would normally use the session_decode() function to decode the data, but that would populate $_SESSION with the data, which is probably not what you're trying to do. There is some user-submitted discussion in the PHP manual about decoding session data: http://php.net/manual/en/function.sessio...php#108037
Reply




Theme © iAndrew 2016 - Forum software by © MyBB