View Full Version : php help!!
Mohd
July 26th, 2002, 21:24
how to show the number of the people who is viewing a page
for example ngmu home page
Like in psxfanatics http://www.psxfanatics.com
Mohd
July 27th, 2002, 14:20
No Answer Please I need this help . .
Nezzar
July 28th, 2002, 09:47
Nobody? OK, then I'll do it.
First you'll need something that stores the guys that are online. Preferrably a Database.
The structure of the DB-Table should go like the following
sessionid (primary key) -> char(32)
lastactivity (i use unix-timestamps for this) ->int(10)
Now, when the user comes to a new page you check if he's already in the table:
if(!mysql_query("SELECT lastactivity FROM sessions WHERE sessionid = '".session_id()."'") {
mysql_query("INSERT INTO sessions VALUES('".session_id()."','".time()."')");
} else {
mysql_query("UPDATE sessions SET lastactivity = '".time()."' WHERE sessionid = '".session_id()."'");
};
Somewhere at the end of the page you have to do a garbage collection to remove all the old sessions:
mysql_query("DELETE FROM sessions WHERE lastactivity > ".time() - 1800);//time() + 1800 = now - half an hour
To check the how many users are online u need this query:
$res = mysql_query("SELECT count(*) AS count FROM sessions");
list($count) = mysql_fetch_array($res);
echo "There are $count users online";
Mohd
July 29th, 2002, 13:52
Thanks Nezzar you finally back to here . . .
Nezzar
July 29th, 2002, 19:11
I just found a bug (it's emberrassing to make errors in only 5 lines of code :p):
The Second Query must look like this:
mysql_query("DELETE FROM sessions WHERE lastactivity > ".(time() - 1800));//<- notice the extra brackets around time() - 1800
Mohd
July 29th, 2002, 20:24
Thanks again done it now
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.