whosonline scripting
Moderator: General Moderators
whosonline scripting
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
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
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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
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
?>
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
?>
hey
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
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
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
hey
thank you for that script
I have some questions:
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
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')");
}
?>Also, is the added db connect code, necessary here?
And, does the above require any additional code?
thank you man, I appreciate the examples
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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
then loop them out
and then to get the total users online # do a query like
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());
?>Code: Select all
<?
while ($user = mysql_fetch_array($result))
{
echo "<a href='profile.php?".$user["id"]."'>".$user["username"]."</a>'
}
?>Code: Select all
<?php
$result = mysql_query("SELECT DISTINCT * users_online") or die(mysql_error());
?>you can mark this solved
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
ok
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
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
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)
);Im just setting flags to see if someone is online or not, do I need all those fileds???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) );
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
That was just an example. But you will need at least 3 fields.
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.
Code: Select all
userID
lastSeen
IPYou may also want to hold the address of the page they loaded so people can see what other people online are currently looking at.
ok
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:
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 
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");
?>-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
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.