#1
  1. action=(isSleep())?sleep:code;
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Sydney->Indiana
    Posts
    182
    Rep Power
    15

    Question finding who's online


    I've seen this question and quite a few answers in PHP and I'm getting some ideas for doing it in python. That is finding out who's online at any particular time on a particular webpage.

    I'm guessing I'll be using some kind of temp file and the os.environ["REMOTE_ADDR"]
    function, but I was wondering if anybody had any ideas or snippets of success before?

    Thanks,
    Benjamin
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    "online at any particular time on a particular webpage"? You mean like on here? The only real way to do it, unless there's some sort of other way to access that data, is to just parse the webpage for it.
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  4. #3
  5. action=(isSleep())?sleep:code;
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Sydney->Indiana
    Posts
    182
    Rep Power
    15
    Yes, online at that particular moment for that webpage, similar to the way devshed's forums have at the bottom user's and who they are. Any ideas?

    Thanks,
    Benjamin
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Surly to get the info your program would have to be on the webserver your tageting.

    That is finding out who's online at any particular time on a particular webpage
    it's got to be hard if not impossibe to just pick a website and get a list or remote IP's on that page.. maybe you could post a few links to the idea's you were given on how to do it in PHP?

    Mark
    programming language development: www.netytan.com Hula

  8. #5
  9. action=(isSleep())?sleep:code;
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Sydney->Indiana
    Posts
    182
    Rep Power
    15
    Sorry should have made that clear, it's for my own site. It's got php, so I could use that, but I'd rather do it in python as not only do I like python more, I've already got python code running.

    So yes, I've got access to the server, all the cgi variables and wish to go about doing something like this(php, sorry for the length) get's who's online using files:

    PHP Code:
    <?php
    //vars
    $file_name "data.txt";
    $c_time time();
    $timeout 300;
    $time $c_time $timeout;

    //write to file ip and time
    $fp fopen($file_name"a");
    $write $REMOTE_ADDR."||".$c_time."\n";
    fwrite($fp$write);
    fclose($fp);

    //open file into array
    $file_array file($file_name);
    $online_array = array();//create online array
    foreach($file_array as $data){
    list(
    $ip$user_time) = explode("||"$data);
    if(
    $user_time >= $time){
    array_push($online_array$ip);
    }
    //end if
    }//end foreach

    //count and output data
    $online array_unique($online_array);
    $online count($online);
    if(
    $online == "1"){
    echo 
    "User online: $online";
    }else{
    echo 
    "Users online: $online";
    }
    ?>

    in python.
    Cheers,
    Benjamin
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    OK, this should do the same thing! it's untested and could be improved a little but hey

    Code:
    import os, time
    
    filename = 'data.txt'
    timenow = time.time() - 300
    online = {}
    
    file(filename, 'a').write(str(os.environ('REMOTE_ADDR')) + '-' + str(time.time()) + '\n')
    
    for user in file(filename, 'r').readlines():
    	user = user.strip().split('-')
    	if user[1] >= timenow:
    		online[user[0]] = user[1]
    
    users = ', '.join(online.keys())
    	
    if len(online.keys()) == 1:
    	print 'User online:', users
    else:
    	print 'Users online:', users
    Have fun,
    Mark.
    Last edited by netytan; October 8th, 2003 at 10:23 AM.
    programming language development: www.netytan.com Hula

  12. #7
  13. action=(isSleep())?sleep:code;
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Sydney->Indiana
    Posts
    182
    Rep Power
    15
    Thanks Mark, I'll have a play and check it out.
    Cheers,
    Benjamin

IMN logo majestic logo threadwatch logo seochat tools logo