Computing.Net > Forums > Web Development > Whos Online

Whos Online

Reply to Message Icon

Original Message
Name: Jamie_McCoy
Date: March 9, 2005 at 17:38:34 Pacific
Subject: Whos Online
OS: XP Pro 5.1
CPU/Ram: Pentium 4 2.4Ghz
Comment:

i want to add a whos online feature to my website...

i am going to have an invisible frame used to reload the page ervery 60 secs or somethin, which will excute code that inserts their username in a text file, if the username already exist in the file then it does nothin...

that part works great, however, how can i get that username removed from they text file when the exit my site.. if i can figure out that i can code the script...


roamhosting.net


Report Offensive Message For Removal


Response Number 1
Name: Laler
Date: March 9, 2005 at 20:14:43 Pacific
Subject: Whos Online
Reply: (edit)

hi,

to know when someone "leave" the site is hard to do I guess, unless the connections are persistent.

why don't you remove the username after a certain amount of time? most "who's online" scripts does this, like phpBB: "there're ... user online in the past 5 minutes"

about frame IMO it's also not too reliable (for the same reason as javascript). so, this is what phpBB and similar scripts do:

- everytime someone click on something, the script is called (include the script to every page on the site).

- the script will insert username and timestamp to db, if already exist remove the old one.

- the script checks for current usernames and timestamp on file and remove usernames with timestamp more than (let's say) 5 minutes ago

- generate reports (how many users in the past 5 minutes, what are the usernames, etc)

how's that sound? :D


---
siggy space for rent


Report Offensive Follow Up For Removal

Response Number 2
Name: Jamie_McCoy
Date: March 10, 2005 at 08:13:12 Pacific
Subject: Whos Online
Reply: (edit)

hey man good advice, however i am against mysql altogether... i like to code everythin in PHP... to increase my knowledge even if it is the hard way around things.... and if i ever wanted to move then i wouldnt have top mess about wth MYSQL

anyway, this time stamp is what im after, but the PHP way. the thing that inserts their username into a file after every click is how im going about it, but i need to take it out.

perhaps i could have it insert some PHP after each click with a time stamp related to their username

but thats the hard bit, any more info of how to do this in PHP

roamhosting.net


Report Offensive Follow Up For Removal

Response Number 3
Name: Laler
Date: March 10, 2005 at 08:51:26 Pacific
Subject: Whos Online
Reply: (edit)

am not saying anything about mysql :P~

here's some code. I have a strong feeling that this won't work on the first try but Im sure you can work it out:

<?php
// helps debugging a lot
error_reporting (E_ALL);

// get db
$opFile = fopen ("db.txt", "r");
$handle = fread ($opFile, filesize ("db.txt"));
fclose ($opFile);

// explode db by lines
$lines = explode ("\n", $handle);

// helps debugging a lot also, change $lines into anything
echo '<pre>'.print_r ($lines, TRUE).'</pre>';

// look if username already in db and if it's more than 5 minutes ago
foreach ($lines as $key => $line){

// explode the line because each line contains username & timestamp
$usernameondb = explode ("|", $line);

// if found, remove it
if (($username == $usernameondb[0]) or (time () - 300 >= $usernameondb[1])){
unset ($lines[$key]);
}

}

reset ($lines);

// put username to db handle
$lines[] = $username."|".time ();

// do the counting here, put into a variable to be called later somewhere on the page... also maybe fetching the usernames to show who's online...

// put back to file, make it into string first
$handle = implode ("\n", $lines);
$opFile = fopen ("db.txt", "w");
fwrite ($opFile, $handle);
fclose ($cache_opFile);
?>

there're maybe other scheme to make the script faster

time(); is unix timestamp -> the amount of second since january 1st 1970

please post back if you find some trouble

---
siggy space for rent


Report Offensive Follow Up For Removal

Response Number 4
Name: Laler
Date: March 10, 2005 at 08:55:52 Pacific
Subject: Whos Online
Reply: (edit)

fclose ($cache_opFile);

should be:

fclose ($opFile);

---
siggy space for rent


Report Offensive Follow Up For Removal

Response Number 5
Name: Jamie_McCoy
Date: March 10, 2005 at 09:35:32 Pacific
Subject: Whos Online
Reply: (edit)

i will have a look into that later...

did you write that your self ?


roamhosting.net


Report Offensive Follow Up For Removal


Response Number 6
Name: Laler
Date: March 11, 2005 at 06:09:22 Pacific
Subject: Whos Online
Reply: (edit)

well yes but I'm sure there're a lot of similar scripts on hotscripts.com but sometimes you just want to code it yourself so you can tune it as you like right? :P

above is basically just a sketch... you need to work on it a lot before it can work precisely as expected...

just keep checking the print_r ($vars, TRUE) and also viewing the text file, after each call to the script. to see if you're moving in the right direction...

---
siggy space for rent


Report Offensive Follow Up For Removal

Response Number 7
Name: Laler
Date: March 11, 2005 at 07:05:07 Pacific
Subject: Whos Online
Reply: (edit)

ok here it is, sorry it's in nother place to make it easier to edit:

http://iix.broadband.or.id/forum/viewtopic.php?t=31

please be patient when accessing it because the bandwidth to/from outside the country is capped at a scary level.

it (when I code something) usually still have some bugs that will be discovered after about a few days :D

> it uses IP as the "user"

---
siggy space for rent


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Whos Online

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge