SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    user's status online or offline

    Hello,

    May be this is already discussed here. but i didn't get anything related to it though(solved one). so sorry if this would become repetitive thread.

    I want to show user's status online or offline. so i have created one status column into my database. if user is login change the status to 1. till here i am done. but how can i update that 1 to 0 while i ll logged out.

    because in my logout.php, only following code is there
    PHP Code:
    $retval retpath();
    session_start();
    $userid $_SESSION['userid'];
    $login_date date('Y-m-d');
    $_SESSION['lastpath']=$_SESSION['lastpath']."\n".$_SERVER['HTTP_REFERER']."\n".$_SERVER['PHP_SELF'];
    $user_filename "log_files/user-".$userid.".log";
      
    $user_file=fopen($user_filename,"a+");

    unset(
    $_COOKIE['userid']);

    //this is something i want to do
    $update $op->runsql("UPDATE ".tbl_author." SET status = 0 WHERE id = '$userid'");


    unset(
    $_COOKIE['lastpath']);
    unset(
    $_COOKIE['lasturi']);
    session_destroy(); 
    if we are able to do this. then there is one more issue will be remaining.
    what if user wont do log out and just close the browser as it is then how come my status column would be updated?

    please suggest me something.
    thanks in advance.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Don't have a status column, have a "last access" column. Update that column on any page load of your site with the current time.

    To determine who is online, select all rows where "last access" is within X minutes. You pick an arbitrary time, like 20 minutes, after which you assume the person has left your site since you can't really know.

  3. #3
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your reply Dan Grossman,
    your are right this can be solved my 2nd issue as well where if user left the page without logged in. right?

    ok, as you suggested, do i have to do somthing like this :-

    database

    field name type
    last_access TIMESTAMP
    query would be like this

    PHP Code:
    $query  =  mysql_query("SELECT usernae,TIMESTAMPDIFF(MINUTE, last_access, NOW() > 20) from 
    author where id ='
    $userid");
    if(
    mysql_num_rows($query) > 0){

    echo 
    "online"
    }
    else
    {
    echo 
    "offline"
    }
    // i am considering 20 minutes here. 
    please tell me is this something you are talking about?

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, this is the solution to not knowing when someone leaves the site when logging out, and that's the kind of query you'd use to know who's online (but < instead of >).

  5. #5
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    database
    field name type
    last_access datatime
    not using timestamp. because its giving me updated on current timestamp. that i dont want. is it ok?

    here is code. it showing me user as offline. i think query is wrong. could anyone help me. with my query.

    PHP Code:
    $update $op->runsql("UPDATE ".tbl_author." SET last_access = '".date('Y-m-d H:i:s')."' WHERE id = '$uid'");
    //  if(mysql_num_rows($update) > 0){

    $result            $op->runsql("SELECT TIMESTAMPDIFF(MINUTE, a.last_access, NOW() > 10)  from
    "
    .tbl_author." as a where a.id ='$uid");
      if(
    mysql_num_rows($result) > 0){

      echo 
    "online";
      }else{

      echo 
    "offline";
      }
    //} 
    Last edited by deepM; May 21, 2009 at 03:37.

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You want rows where the difference between now and the last access is LESS than 10 ("within the last 10 minutes"), not greater.

    But your code will then always write online since you update the row in that same page.

    I'd use a DATETIME column myself.

  7. #7
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    You want rows where the difference between now and the last access is LESS than 10 ("within the last 10 minutes"), not greater.
    i am trying but not getting the proper output. could you please show me the query?

  8. #8
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone help me??

  9. #9
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello, I am back here.

    with help of mysql forum, i am able track user's last access time from now. and if it is more than 15 minutes from now. show that user is offline.

    This we can consider as one condition.

    1) the second condition would be what if user left that page(probably logout ) before 15 minutes so how can i update my status column according to that?

    2)what if user stayed for more than 15 minutes(lets say 2 hours) so did he need to do log in again and again?

    3) As sitepoint shows the logged in users till they themself wont do the log iout/close the browser.

    the solved issue is done with query. so can anyone help me here for how to solve these 2 conditions?

    thanks in advance.

  10. #10
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    United States
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) i wouldn't worry about this. When it was last updated, and he leaves, after 15 minutes he would be considered offline.. You don't need to change the field. If you want to, You can update the datetime field you created when they log out.

    2) No, as long as you are updating the datetime field for the user with every page load, you'll be fine..

    3) Don't understand. I wouldn't concern myself with it.


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
  •