SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2011
    Location
    zziapex.com
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get online user list and remove unactive user from list

    Hello

    I am try to make function how get currently online user on my website...
    I am saving user_id in table but i am facing one problem when i close my browser without logout my system still showing me login because my user_id is store in DB.

    how can i know that the user is actually online means how can i get session is set or destroy.

    or if you can best to get online user please tell me.

    you can see get my website links from my signature and you can find login user below footer

    Regards

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,162
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    You should be wiping out any session/cookie data during the logout process, so when they click logout, you should be deleting cookies and using unset($_SESSION) to wipe out the session variables.

  3. #3
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    It looks like he's talking about when the browser is closed without the user logging out.

    Unfortunately 12345qwer, there is no way to detect a closed browser in PHP. There are some JavaScript/AJAX methods but they're far from fool proof. Your best bet is to add something like a last_activity datetime field to your "active users" table in your DB. Then, whenever a logged in user does anything on your site (views a page, makes a post, etc), update your DB's last_activity field with the new timestamp.

    Then you have two options.
    If you have MySQL 5.1.6 or greater, you can set up an event in your DB to run say, every 15 minutes to check for users who have been inactive for however long and remove them from your active users table. Events in MySQL are the MySQL equivalent to linux cron jobs only they're managed and run by the DB server specifically for DB tasks.

    If your version/setup doesn't allow for Events, then set up a cron job to run say, every 15 minutes and call a PHP script to do the same.
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by kduv View Post
    It looks like he's talking about when the browser is closed without the user logging out.

    Unfortunately 12345qwer, there is no way to detect a closed browser in PHP. There are some JavaScript/AJAX methods but they're far from fool proof. Your best bet is to add something like a last_activity datetime field to your "active users" table in your DB. Then, whenever a logged in user does anything on your site (views a page, makes a post, etc), update your DB's last_activity field with the new timestamp.

    Then you have two options.
    If you have MySQL 5.1.6 or greater, you can set up an event in your DB to run say, every 15 minutes to check for users who have been inactive for however long and remove them from your active users table. Events in MySQL are the MySQL equivalent to linux cron jobs only they're managed and run by the DB server specifically for DB tasks.

    If your version/setup doesn't allow for Events, then set up a cron job to run say, every 15 minutes and call a PHP script to do the same.
    Well put. I would lean to toward the cron job in case you have any other tasks you may want to accomplish that may not be suited for MySQL.

  5. #5
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    Well put. I would lean to toward the cron job in case you have any other tasks you may want to accomplish that may not be suited for MySQL.
    Yeah, a cron may be better for this type of thing. After all, this type of thing would fall under "application logic" and probably shouldn't be the job of the DB.
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  6. #6
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by kduv View Post
    Yeah, a cron may be better for this type of thing. After all, this type of thing would fall under "application logic" and probably shouldn't be the job of the DB.
    Agreed. Thinking on it further, one could log activity in a table and then present an active user list using criteria on last active, then running a cron job once a day to clean that table up. Might be a better idea than relying on that cron job to do its job to present the correct data, a cron job failure here would only result in wasted table space.

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,162
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kduv View Post
    It looks like he's talking about when the browser is closed without the user logging out.
    Thanks, I missed that part.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2011
    Location
    zziapex.com
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kduv View Post
    It looks like he's talking about when the browser is closed without the user logging out.

    Unfortunately 12345qwer, there is no way to detect a closed browser in PHP. There are some JavaScript/AJAX methods but they're far from fool proof. Your best bet is to add something like a last_activity datetime field to your "active users" table in your DB. Then, whenever a logged in user does anything on your site (views a page, makes a post, etc), update your DB's last_activity field with the new timestamp.

    Then you have two options.
    If you have MySQL 5.1.6 or greater, you can set up an event in your DB to run say, every 15 minutes to check for users who have been inactive for however long and remove them from your active users table. Events in MySQL are the MySQL equivalent to linux cron jobs only they're managed and run by the DB server specifically for DB tasks.

    If your version/setup doesn't allow for Events, then set up a cron job to run say, every 15 minutes and call a PHP script to do the same.

    thanks for your help..now i can do this...
    I don't know about cron job can you tell ?

    I am getting data using ajax my page area here my online will be show is refreshing after 10 seconds.

    Regards


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
  •