CodeIgniter Forums
Yet another problem with Session with DB - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Yet another problem with Session with DB (/showthread.php?tid=40803)



Yet another problem with Session with DB - El Forum - 04-19-2011

[eluser]R_Nelson[/eluser]
this is how i set the DB session
Code:
function is_logged_in()
    {
        $CI =& get_instance();    
        $is_logged_in = $CI->session->userdata('is_logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true)
        {                
            return false;
        }else{
            return true;
        }        
    }
it works just fine but what i'm having problems with is if i shut down firefox and come back in i have 2 sessions in the DB or if i login via IE or another computer what i would like to do is purge the older one from the database i know how to remove it but i need help with checking to see if there is more than 1 and witch one is older!


Yet another problem with Session with DB - El Forum - 04-19-2011

[eluser]WanWizard[/eluser]
Garbage collection is run automatically at set times by the session class, nothing you should be handling yourself. By default every session library load there's a 1 in 20 chance it will start.

You can alter that by using a MY_Session:
Code:
class MY_Session extends CI_Session
{
    var $gc_probability = 5; // any value between 0 (never) and 100 (always)
}



Yet another problem with Session with DB - El Forum - 04-19-2011

[eluser]R_Nelson[/eluser]
ok but garbage colection only works on anything older than 10 mins what im trying to do is if say user foobar is logged in then he gets on his laptop and logs in i want it to destroy the first session! so he cant be logged in on the same account twice!


Yet another problem with Session with DB - El Forum - 04-19-2011

[eluser]WanWizard[/eluser]
I don't see the point of doing that. What if the user starts two browsers on the same PC? What if the user opens multiple browser windows or tabs (=same session!)?

But if you insist, either extend the Session library to include the user_id to the session record, or construct a delete query with a where clause that selects all records with a specific character sequence in the user_data (make sure that you don't select the wrong records).


Yet another problem with Session with DB - El Forum - 04-20-2011

[eluser]R_Nelson[/eluser]
the main reason i want it to only have one instance in the db is because i want to use it for a who is online and i don't want 20 instances of the same person! Since i have no idea how i would extend the class i guess setting a delete function would be best!


Yet another problem with Session with DB - El Forum - 04-20-2011

[eluser]WanWizard[/eluser]
If you include the user (some form of unique id), all you need to do is to run a DISTINCT query, and include MAX(last_activity) to get the most recent active one.

This doesn't say much about being online though, because it's not updated at every page request, only when the session_id rotates.


Yet another problem with Session with DB - El Forum - 04-20-2011

[eluser]R_Nelson[/eluser]
so are you saying i should just create another DB that i update every time a logged_in page is loaded?


Yet another problem with Session with DB - El Forum - 04-20-2011

[eluser]WanWizard[/eluser]
No, you could use the session table for that, but it requires some modifications.

Best thing to do is to copy the session library to application/libraries, then modify it to include a last_updated timestamp (that get's updated every UPDATE query), and a user_id field, that you set from your is_logged_in() method (use 0 if no user is logged in).

Once you have that, you can run a query like
Code:
SELECT user_id, MAX(last_updated) as timestamp FROM sessions WHERE user_id > 0 GROUP_BY user_id
to get the timestamp of the last page request for every user.


Yet another problem with Session with DB - El Forum - 04-20-2011

[eluser]R_Nelson[/eluser]
I had a friend who knows PHP better than me and he said what you suggested is a good idea but considering i have all my logged in pages on the same controller i should just a separate table and put it in the constructor!

thanks for the help tho!


Yet another problem with Session with DB - El Forum - 04-21-2011

[eluser]WanWizard[/eluser]
Possible, buf effectively you'll have to make a second session manager. You'll have to take care of linking the page request to the session to the user to a record in your new table. You're have to do garbage collection yourself, etc.