Welcome Guest, Not a member yet? Register   Sign In
Using sessions with database..
#1

[eluser]GonzaFY[/eluser]
I am here with some questions again.. Sorry for that but this is the best forum to question lol Tongue.
Sorry for my english.

Well I finally understood how works the sessions with database..
If I am not wrong, CI makes a session id each time anyone visit the page and save that into database and in a cookie. Every time that you decide, it refresh and check if the cookie has the same id as the database, if it isn't the session is destroyed.

Then I made the table into my mysql, now I have two tables: Members (to check the login and store the user's data) and CI_Sessions (to check the sessions and store the user's data that I already called when a user log).

I wanna know if I am doing something wrong because it seems too easy to use..

In my code, when a user log in, it check if the username and the password is the same with some row into the database, if this is true then I get all the data from the database (name, surname, address, level, etc) and store this in sessions (CI_Sessions database).
After I call to those sessions to get information about the user.. ($this->session->userdata('address')Wink

Is that ok?

And another question, How can I check if a third user is online?
I thought having another column in my "Members" table called "Status" and then when the user is logged I make a call to sessions and will be true, if he is online change "Status" to Connected. All is fine now but I don't know how change "Status" it to "offline" if his session is destroyed because expired..
Anyone have idea? Is this the best way to do that?
EDIT:
Another way will be using a php file that refresh every X minits and check the last activity of a user and if it reached more than 5 minits (for example) it return "OFFLINE" but I don't understand the column "Last_Activity".. It return "1360644145" .. What is that?


Ok the solution is:
Code:
public function Estado($usuario)
  {

// We get the LAST session of the $usuario
   $funcion = $this->CI->db
         ->like('user_data', $usuario)
         ->limit(1)
         ->order_by('last_activity', 'desc')
         ->get('ci_sessions');  
// If there is one...        
   if($funcion->num_rows() > 0)
   {
    $cuenta = $funcion->row()->last_activity;
    $now = time();

// We check if he is active..
    if($cuenta + 100 > $now)
    {
     echo 'ONLINE';
    }
    else
    {
     echo 'OFFLINE';
    }
    
   }
   else
   {

   echo 'OFFLINE';
   }
  
  
  
  }

Sorry for my english =/!
#2

[eluser]InsiteFX[/eluser]
To update a user online status you would need to use javascript timer events.

For the database you would need to create a user_online table and store the
information in it.

#3

[eluser]GonzaFY[/eluser]
[quote author="InsiteFX" date="1360678038"]To update a user online status you would need to use javascript timer events.

For the database you would need to create a user_online table and store the
information in it.

[/quote]

Do you mean a third database?
And why can't I store the information into ci_sessions?


I was thinking and I deduced I need do something like the facebook chat, if the user has the window opened then he is connected, if he close the browser he become off... But I don't want destroy the session when he close.

Thank Big Grin


Then now I am trying to use the sess_expire_on_close to set off a user (but not expire the session) but when I search into the core I found only that:
$expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();

I wanna make when sess_expire_on_close is called make "Status" to 0 but I don't understand how do that..




Theme © iAndrew 2016 - Forum software by © MyBB