SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 27
  1. #1
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Logout PHP Ideas needed

    I have a chat script that i made but i need ideas on removing users who are not on the webpage, right now if they login and close the browser they still stay in the chat, and to logout they need to click the logout link, what is some ideas to make users logout from the server.
    Animated Chatrooms - www.121chatrooms.net

  2. #2
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I don't know what you mean? They already have a log out link you said? What's wrong with that?

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Are the logged-in users stored as a value in a database?

    As the page refreshes every few seconds, you could run an update query on every load, updating the last time a user was logged in. Then, whenever you run the query to find out who is logged in, instead of looking if they are logged in, look for the last time they were logged in - making sure it was less than say 20 seconds ago.

    That way, when the page reloads it sets a value in the database with the time, and if the user closes the page, it doesn't reload therefore doesn't set the value in the database.

    @stormrider - I think the issue (though I may be incorrect) is that people close the window instead of clicking "logout".
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well if they close the browser and they dont click the logout link they will be shown as logged in.
    Animated Chatrooms - www.121chatrooms.net

  5. #5
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is how my chat with the database is setup.

    When they login the script adds an insert qry to the onlineusers field when they logout if deletes them from the onlineusers table.
    Animated Chatrooms - www.121chatrooms.net

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What's the layout of this table?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do you mean the type as in mysql or type as in all the field names?
    Animated Chatrooms - www.121chatrooms.net

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    field names.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    username, level, roomlocation, roomcat, ignoreprvreq, setaway, ispremium, ip
    Animated Chatrooms - www.121chatrooms.net

  10. #10
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Could you add a column:

    username, level, roomlocation, roomcat, ignoreprvreq, setaway, ispremium, ip, lastactive (timestamp)

    and modify the query to see if a user is logged in to add:
    Code:
    WHERE (lastactive - NOW() < 60)
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  11. #11
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok im in phpmyadin and i added a new field and type i clicked timestamp but in default i have a check box to tick the option current timestamp, do i check that optipn?
    Animated Chatrooms - www.121chatrooms.net

  12. #12
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yeah.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  13. #13
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i have created the field, so on my chat i make it update everytiem the chat refreshes how can i make it delete the users from the table that are not online after 60 seconds.
    Animated Chatrooms - www.121chatrooms.net

  14. #14
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You would make a cron-job to run a clearing script at night.

    However, before then, you only select rows from the table with the last login time less than 60 seconds, as I said before.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  15. #15
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That would just mean someone would log in, start chatting, and get logged out after 60 seconds...

  16. #16
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    From what I can gather by looking at the forum, the room is within an iFrame which refreshes with new messages every couple of seconds - so it will keep updating the last refresh of the user.

    If it was more than 60 seconds ago the iframe doesn't refresh therefore the window is closed, therefore the user isn't active.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  17. #17
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You could always use AJAX to fire off a request to another script, which would alter the DB and keep the user logged in.

    Using the additional field recommended by arkinstall, create a script which would update the last active field based on the users id.

    You would then just need a small bit of JS to silently update the user's activity time, and possibly expire others.

    My 2 cents.

  18. #18
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you please explain your idea arkinstall with more detail im kind of understanding what to do.

    thanks.
    Animated Chatrooms - www.121chatrooms.net

  19. #19
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to update the 'lastactive' column each time the page chat refreshes. So as long as the chat continues to refresh, the user will stay logged in. If the user closes the browser, then the 'lastactive' column will not be updated.

    Make sure to only show users in chat that have a lastactive <= 60 seconds.

  20. #20
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how do i add to this $onlineusersquery = mysql_query("SELECT * FROM onlineusers WHERE username='$username' ") so i can add WHERE (lastactive - NOW() < 60) to it
    Animated Chatrooms - www.121chatrooms.net

  21. #21
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, have you tried the obvious?
    PHP Code:
    $onlineusersquery mysql_query("SELECT * FROM onlineusers WHERE username='$username' WHERE (lastactive - NOW()) < 60"); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  22. #22
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mike4x4s View Post
    how do i add to this $onlineusersquery = mysql_query("SELECT * FROM onlineusers WHERE username='$username' ") so i can add WHERE (lastactive - NOW() < 60) to it
    Try this:

    Code:
    SELECT * 
    FROM onlineusers 
    WHERE 
    username='$username' 
    AND (lastactive - NOW()) < 60
    I'm not sure how MySQL represents time, in terms of minutes and seconds.... Will "60" represent 60 seconds?

  23. #23
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's stored as seconds since 1st Jan, 1970.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  24. #24
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its stored as 2008-04-30 07:27:11
    Animated Chatrooms - www.121chatrooms.net

  25. #25
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is my code atm nothign seems to poutput formt he db:

    PHP Code:
    image code here<br></br>
    <body bgcolor="#EFEEFC">
    <font size="2">

    <?php
    include "global.php";

    $query mysql_query("SELECT * FROM onlineusers WHERE username = '$username'")
    or die(
    "Could not insert data because ".mysql_error());
    $qry mysql_fetch_array$query );
    ?>

    <form action="search2.php" method="post">
    <br>
    <img src="images/rankicons/member.gif"> Username: <input type="text" name="username" value=""><input type="Submit" value="Search">
    </form>

    <?php

    $result 
    mysql_db_query($dbname"SELECT username FROM onlineusers WHERE
    username='
    $username'
    AND (lastactive - NOW()) < 60 "
    ) or die (mysql_error());

    if (
    mysql_num_rows($result)) {
       while (
    $qry mysql_fetch_array($result)) {
       
    $query2 mysql_query("SELECT * FROM onlineusers WHERE
    username='
    $username'
    AND (lastactive - NOW()) < 60"
    )
       or die(
    "Could not insert data because ".mysql_error());
      
    $qry2 mysql_fetch_array$query2 );
      
    $query3 mysql_query("SELECT * FROM onlineusers WHERE
    username='
    $username'
    AND (lastactive - NOW()) < 60"
    )
      or die(
    "Could not insert data because ".mysql_error());
    $qry3 mysql_fetch_array$query3 );

    switch (
    $qry2[level]) {
    case 
    'guest':
    $link "$qry[username] [$roomlocation$qry2[roomlocation]]";
    break;
    case 
    'member':
    $link "<a href='viewprofile.php?username=$qry[username]'>$qry[username]</a> [$roomlocation$qry2[roomlocation]]";
    break;
    case 
    'lmod':
    $link "<a href='viewprofile.php?username=$qry[username]'>$qry[username]</a> [$roomlocation$qry2[roomlocation]]";
    break;
    case 
    'mod':
    $link "<a href='viewprofile.php?username=$qry[username]'>$qry[username]</a> [$roomlocation$qry2[roomlocation]]";
    break;
    case 
    'smod':
    $link "<a href='viewprofile.php?username=$qry[username]'>$qry[username]</a> [$roomlocation$qry2[roomlocation]]";
    break;
    case 
    'admin':
    $link "<a href='viewprofile.php?username=$qry[username]'>$qry[username]</a> [$roomlocation$qry2[roomlocation]]";
    break;
    }

    echo 
    "<img src='images/rankicons/$qry3[level].gif'>  $link<br>";
    }
    }

    ?>
    </font>
    Animated Chatrooms - www.121chatrooms.net


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
  •