SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    session data and garbage collection

    I have a web app that I am building. I have sessions used where users can check a remember me box at login. The way I am handling that is to save a cookie that holds the gc_maxlifetime value of 31 days from the time they logged in. The cookie itself expires at the same 31 days. The reason I did this is because the php.ini is set to default the value to 14400 which it does if I don't explicitly set it each time (thus the cookie value). All of this works fine. My problem comes in when I want to retrieve the number of users that are on the site. Say a user chooses the remember me option. They then spend their time on the system, and then instead of logging out, they just close the browser and shut down the machine. This then causes an incorrect online user count because the user is no longer online, but their previous session is set to expire in a month. So for the month, the users session is still held in the system, throwing the count off. Is there a way that I can detect if a user has closed out of the system?

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually the way of finding the online users is little different in my method. What i do is:
    - Have a flag field which holds Yes/No after the user logged in it is set to 'Yes'. Upon manually logs out then the flag will be set to 'No'.
    - And another datetime field to hold the user's logged in time.

    Then when counting the number of users online, just check the the flag along with the time when the user has logged in last 5 minutes (with the interval of minutes/seconds).
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rajug, I liked that idea and tried it. I added the boolean field "logged_in" to my user data table. I already had a field called "last_on", I just changed the code for that to update whenever a user accessed a page. I then wrote a simple query to count the number of users that are marked as being logged_in and have had logged activity within the last 5 minutes. It appears that this will work well for what I am doing.

    I then query the session data table for users that are not logged in and use that as the count of guest users that are online since my default session time is short.

    A few other comments on your reply
    I have gotten in the habit of using unix time when I store data in the database. 1227908033 is a lot shorter to store in the database than 2008-12-05 14:08:58, and can be converted to any date format down to the second. Along the same lines of thinking, I store all my yes/no answers in a tinyint(1) field as 1 or 0. Not only does it take less space in the database, but 1 and 0 are more easily processed in PHP as they automatically evaluate to true and false respectively.

    Anyways, thanks for the guidance.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •