whosonline scripting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

whosonline scripting

Post by fresh »

hello,

how would I go about coding a whosonline script? I suppose I need to first connect to my db but how would I enumerate the archive? Also, I am only wanting to list like the top 5 in the form of LIFO, last in first out. I would like it to refresh the list every 5 minutes and allow for users to click their name and be brought to their account page... I can cover that... but how would I code something like a whosonline script... thanks
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what I would do it set a flag when they login that will time out in 5 minutes..


so they are logged in... set flag for 5 min
they browse around each page.. each time resetting the 5 min

then in your query

Code: Select all

<?php

$result = ("SELECT * users` WHERE flag > timestamp  ORDER BY flag LIMIT 5");

?>

then loop out the results to get the users....

and inside the loop have

<a href="profile.php?id='<? echo $row["id"]; ?>'>link</a>



obviously a lot of changes ahve to be made... jus giving you somewhere to start


?>
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

hey

Post by fresh »

good ol' google... already went that route...

so basically I just connect to mysql, select the db and loop threw the tables... flag? how do I set a 'flag'? Also, how would I throw those in a loop, as a variable? I am pretty new still, haven't done anything like flag setting or much sql querying yet... little more help on the syntax? thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

hey

Post by fresh »

thank you for that script :) I have some questions:

Code: Select all

<?php
$db_engine = 'mysql';
$db_user = 'theusername';
$db_pass = 'thepassword';
$database = 'my_db';
$server = 'localhost';

//get the time variables
$utime=time();
$exptime=$utime-300; // (5 minutes in seconds)

//delete expired flags from ONLINE table
$delete = @mysql_query("DELETE from online WHERE timevisit<$exptime") or die(mysql_error());

//select current online users from USER TABLE
$select = @mysql_query("SELECT DISTINCT * user_table WHERE loggin='set'") or die(mysql_error());

$user = mysql_fetch_array($result);

if (mysql_num_rows($result) > 0 )

@mysql_query("UPDATE online set timevisit='$utime' WHERE visitor='$uvisitor'");
} else {
@mysql_query("INSERT into online (visitor,timevisit) values ('$uvisitor','$utime')");
}
?>
So I make two tables, user and online, right? not users and online vars??

Also, is the added db connect code, necessary here?

And, does the above require any additional code?

thank you man, I appreciate the examples :)
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

thank you

Post by fresh »

for that link... i learn quickest by example, and I always appreciate a point in that direction... thanks alot!! :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

nono don't use taht script I gave you there.. I wasn't really paying attention to what I was doing.

jus look around hotscripts.. there are many user online scripts

but no matter what you do you will need 2 tables

usersonline
and your actual user table

the idea behind the users online is every time they hit a page their "flag" is refreshed. The flag is a time()-300 (300 being 5 min)... then when someone else a page on the site 5 min later after the user has been inactive.. another query is run to delete the row in users online... this is done by comparing the current time() entered in the db and the current current at the moment of the site being hit.

the concept is really easy... but this is what your going to have to do

when a user logs on do the same concept... they are now signalled as online in users online table.. but for members you always have to set a current time() ( which is updated into their row ) which will be processed in another query which will update their row again if they have been inactive in 5 min.. and if they are not active... set have a column for logged in...

as for listing the members online... simply do a query like

Code: Select all

<?

$result = ("SELECT * users_table WHERE loggedin='set'") or die(mysql_error());

?>
then loop them out

Code: Select all

<?

while ($user = mysql_fetch_array($result))
{
echo "<a href='profile.php?".$user["id"]."'>".$user["username"]."</a>'
}

?>
and then to get the total users online # do a query like

Code: Select all

<?php

$result = mysql_query("SELECT DISTINCT * users_online") or die(mysql_error());

?>
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

you can mark this solved

Post by fresh »

I just decided to emulate it, by creating a file for the user, until the user logs out and the server deletes it, and I just linked to the directory, which brings me to a question and new post... thanks
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

ok

Post by fresh »

I wisened up and am doing this via sql tables now, but I'm not too sure what field means, I can manually make a table on my server and it has two textboxes:

1. Name <-- I know what this means
2. Fields: <-- what's this mean???

could someone clarify the meaning of 'Fields' for me please, thanks ;)
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

I guess its refering to any fields in the table:

Code: Select all

CREATE TABLE some_name_here (
   field varchar(255),
   field_2 varchar(255),
   field_3 text,
   field_4 int(10)
);
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

kettle_drum wrote:

Code: Select all

CREATE TABLE some_name_here (
   field varchar(255),
   field_2 varchar(255),
   field_3 text,
   field_4 int(10)
);
Im just setting flags to see if someone is online or not, do I need all those fileds???
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

That was just an example. But you will need at least 3 fields.

Code: Select all

userID
lastSeen
IP
Then you simply update the lastSeen field each time a user loads a page. If they are a member and logged in then the userID field will relate to their userID, if they are a guest you will have to identify them by the IP address.

You may also want to hold the address of the page they loaded so people can see what other people online are currently looking at.
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

ok

Post by fresh »

well, guests arent allowed in the members section, so they wont need to be tracked, also, I'm not interested in allowing others to see where others are at, in this case, really I just want to see if their online or not and if so, I would like to enumerate that list to my page, so, I would just make one table and one field like so:

onlineusers = table
lastSeen = field

right?

and how would I update this field?? Would I use Phenoms flag set script:

Code: Select all

<?php

$result = ("SELECT * users` WHERE flag > timestamp  ORDER BY flag LIMIT 5");

?>
and do I put this on each page, or just the login page??? Also, do I need anything else, in order to do this correctly... thanks :)
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Well you also need the userID unless you just want to know the number of online users. Then you use the UPDATE sql command to update the table when a user loads a page with the time they loaded the page at. Then to show the list you select all the entries where a page was loaded in the last x seconds.
Post Reply