Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12

Thread: Online tracker

  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Online tracker

    So as some of you may know, I'm working on a text base game right now with my friend. We wanted to be able to find out and show other players how many people are online. First thing I thought of was well, we could have a new field for the users table that is called 'online' if the users online field is set to 1 they are online, if its set to 0 they are offline. Simply by them logging in, it sets the online field to 1, by logging off it sets it to 0... Then I thought.. Well, we are going to have users that says that they are online still because what if they close out the browser without clicking logout...

    So, thats my question.. How would I fix something like this? Is it even possible with PHP?

    Thanks,
    Jon W

  • #2
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Maybe.. Not to sure if this would work. But say everytime they login we could store that online session inside of a session..


    PHP Code:
    if($_SESSION['online'] != 1) {

    mysql_query("UPDATE users SET online='0' WHERE user_id='".$_SESSION['user_id']."'") or die('Server Error: ' mysql_error());


    The question is will this run really fast before it closes.. lol

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    No that wouldn't work. Each session key is individual to the user. You would need a way of seeing how many users have a session key in use. You would need a timeout on sessions. You simply count how many session files there are in the session directory. This isn't going to be completely accurate though you'd need a AJAX function to be completely accurate as to who is online. Something like what facebook do. Which is constantly sending data back and forth.
    You can not say you know how to do something, until you can teach it to someone else.

  • #4
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    I found you a script

    PHP Code:

    /* Start the session */
    session_start();

    /* Define how long the maximum amount of time the session can be inactive. */
    define("MAX_IDLE_TIME"3);

    function 
    getOnlineUsers(){

    if ( 
    $directory_handle opendirsession_save_path() ) ) {
    $count 0;
    while ( 
    false !== ( $file readdir$directory_handle ) ) ) {
    if(
    $file != '.' && $file != '..'){
    // Comment the 'if(...){' and '}' lines if you get a significant amount of traffic
    if(time()- fileatime(session_save_path() . '\\' $file) < MAX_IDLE_TIME 60) {
    $count++;
    }
    }
    closedir($directory_handle);

    return 
    $count;

    } else {
    return 
    false;
    }

    }

    echo 
    'Number of online users: ' getOnlineUsers() . '<br />'
    You can not say you know how to do something, until you can teach it to someone else.

  • #5
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    What type of file would I create to put all of login sessions in?

  • #6
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    you don't need to create a file the sessions are automatically stored on the server in session_save_path. Just try the script :-)
    You can not say you know how to do something, until you can teach it to someone else.

  • #7
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Oh, okay. I got after I started reading up more about it. lol :P


    Thanks for the help.

  • #8
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    The script i posted does work i tested it. Did you try it?
    You can not say you know how to do something, until you can teach it to someone else.

  • #9
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Yes I did.

    This was my error that I got:

    Parse error: syntax error, unexpected T_ELSE in /var/www/learn/case.php on line 23

    I took out the else statement and then I get: Parse error: syntax error, unexpected T_ELSE in /var/www/learn/case.php on line 23

  • #10
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    But I just fixed that. :P So yes, everything is working good.

  • #11
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Okay, I should say it did work.. lol Now I'm getting the fulling error.



    Warning: fileatime() [function.fileatime]: stat failed for /var/lib/php5\sess_1559ea5f36a55e4a77007528f65f4d07 in /var/www/learn/case.php on line 15
    Number of online users: 0


    Any clue?

  • #12
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    hmm. It works fine for me. maybe the stat if failing because your system doesn't record last access time of file? Is it a linux server or maybe a windows server?

    Some things you could try:

    1. make a file somewhere on the server HDD. Access it - open it in a text editor or something. Then test
    PHP Code:
    echo fileatime($full_file_path); 
    obviously replace $full_file_path with the full file path of that file.

    2. If that doesn't work. Then you'll need to forget the timeout functionality of this function i think. Comment the 'if(...){' and '}' lines

    3. If it does work then maybe you need to set session_save_path. Maybe something like session_save_path("/tmp/sessions"); but will need to create that folder on server and set permissions. Where ever you put it don't put it in the doc root of your site. Also you would have to set the session_save_path() on every page that uses your sessions.

    try these things and let me know how you get on. If worse comes to worse I might be able to nick something else from E107 later when I'm at home and see how they count online users.
    You can not say you know how to do something, until you can teach it to someone else.


  •  

    Posting Permissions

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