Ive tried and failed to have a list of active suers on my site. Currenty i am making do with just showing all registered users.

However, when the site gets bigger and (hopefully) more users join, i dont want to have the user list being extra long and taking up the majority of the page!

Is there a simple way I can show only active users? Also, how do i make a session expire or does it do this automatically? Can i use the session expiry to change a database entry to "inactive" for example?

    I would do this:

    Create a database table (e.g., login_sessions) to log users' sessions. An entry is inserted to this table when a user is logged in. The entry (table fields) should include at least username, timestamp, and IP. timestamp can be used to calculate how long this user has been logged in, and IP can be used to obtain more information about location etc. When the user is logged out, the entry will be deleted from the table.

    The problem for this method would be if the user did not manually log out, and close the window, you have no way knowing if he is online or not.

    Maybe PHP SESSION can help.

      yeah, thats what i considered doing. But i know for a fact that it would crash and burn as people wouldnt click logout. Im new to sessions. the only variables i know how to work with are:

      session_start();
      session_register();
      and
      session_unregister():

      I dont see how that could do it for me. This forum has it so I know there is a way, can anyone else help me out? cheers though paull!

        well, I have zero experience doing something like that, but here's my 2 cents anyway ...

        Do exactly what paul088 mentioned, however have 2 timestamp fields. One would contain the time of when they logged-on, the other would contain the time of their last refresh/page change. Just update the second timestamp field everytime they change pages on your site. That way you don't have to delete anyone from the table, instead you can return just the users that have refreshed/changed pages in the last ## minutes. If someone hasn't refreshed or clicked a link on your page in 20-25 minutes, chances are they are gone.

        .. you could also create a userid field in that table, so you can update the info in the table the next time they come to the site, instead of creating redundent times for each user.

          17 days later

          hey, thanks! I hadnt thought of using 2 fields before. The only trouble is... How do i determine how many minutes have passed?

          i use the date(); function and Im having trouble trying to adjust it to display GMT time when my server is something else!
          Any light on how i could tel how many minutes have passed since they last updated the 2nd colomn.

          And also, what would i do if they clicked logout? change th nd field to offline and have MySQL filter that out???

          😃

            You can use the below method for figuring out how many minutes have passed. I'm not sure if it's the best way (maybe there's actually a function that will do it), but it will work atleast.

            $LoginTime = (Timestamp of when they logged in);
            $CurrentTime = time();
            
            $MinutesPassed = date("i",($CurrentTime - $LoginTime));
            

            I'm not sure what I would do when a user logs out. Maybe have a field in the table (as you mentioned) that is set to false when they are offline, and true when they are online. That way you only need to check the rows that have that field set to true. Create a function that checks the two timestamps and sets the value back to false if they have been logged off to long. You might want to think some more on this though. This method would mean you would have to check and update the table everytime every user refreshs the page. It could be a lot of work for the database if your site is busy. Maybe you could do a combination of what paul088 mentioned and what I mentioned. With the 2 timestamps in the database, it would solve the problem he mentioned, however since your using a session variable you wouldn't need to update the field every page refresh. You could just do it every nth minutes.

              thanks! appriciated alot!
              I'll be trying to get this to work today as i have some free time. I'll get back to you aal with how it went! 😃😃

                everytime someone connects to the site, start a session and store his IP in a table with a timestamp refreshed everytime he visits a new page. Then query the table for timestamps < 20 minutes in the past... works quite well here

                Tip : Store your timestamp in an INT field so you can query it without having to play around with date formating.

                  and upon logout DELETE the entry form the table? What if a user doesnt logout? have a function on everypage which deletes session IPs older the 20 mins?

                    yep (second proposal) or just build a clever stats tool (which is what I did that will tell you where and when peeps go and you'll have a nice marketing tool 🙂

                      dunno how i did it but its working!! thanks to all of you! thread resolved!

                        Write a Reply...