
|
View Full Version : [PHP] :) Members Online
JustinSmall 08-02-2008, 01:42 AM Hello, I have a good PHP question... (yes I just asked one already today, but this one is a really good one... many ppl would find it interesting)
I have a user system, controlled by sessions (must be controlled by sessions...)
I want to be able to have a members online thing...
Now I know I could do a mysql insert for time, then on the members online part, just call up by time and if longer than 10 minutes or so say inactive. However, I would like it to be relatively nicer than that... something that is more accurate :)
HELP!!!! :) thanks in advance!
sc_freak 08-02-2008, 04:30 AM If you just want to find out how many members are online during the last 10 minutes, then keep a column in your member table that stores the last activity timestampt. Then query "select all members where last_activity_time > time() - 10 minutes" is sufficient.
If you want to display guests online also, then you might want to have a separate session table with columns like: session_id, member_id (-1 for guest), timestamp.
In your code you need to update the session table every time a page is loaded.
That's how I would do it without knowing your exact need.
jawvvd 08-02-2008, 05:47 AM Also make this a memory-based table, a lot faster then.
Updating otherwise means disk access and that's very costly!
JustinSmall 08-02-2008, 11:49 AM Now I know I could do a mysql insert for time, then on the members online part, just call up by time and if longer than 10 minutes or so say inactive. However, I would like it to be relatively nicer than that... something that is more accurate
I had stated that I didn't want to do a ten-minute based thing... that I wanted the stats to be more up to date and real :P
bager 08-02-2008, 09:20 PM Download some free script like phpbb that has that feature and view the code for online members
Edevere 08-03-2008, 12:17 AM phpBB just counts active sessions too...how else are you supposed to know who is online?
etogre 08-03-2008, 01:21 AM Unless you're gonna have some AJAX call that does a check every other second, or some kind of flash running in the background, I'm not sure if it is possible in PHP/MySQL.
CodyRo 08-03-2008, 01:21 AM I had stated that I didn't want to do a ten-minute based thing... that I wanted the stats to be more up to date and real :P
The issue with this is if someone doesn't reload a page for awhile it will show them as offline.. I would recommend something such as a 5 minute delay.
Edevere 08-03-2008, 02:29 AM I don't really understand what 'more accurate' means in this context. What exactly is the definition of 'active user' if it's not 'user with an active session'? If you're not timing out your sessions in a reasonable amount of time you can check the IP on the session against your logs or something to see who is 'really' active. That's kind of crazy though.
CodyRo 08-03-2008, 03:25 AM I don't really understand what 'more accurate' means in this context. What exactly is the definition of 'active user' if it's not 'user with an active session'? If you're not timing out your sessions in a reasonable amount of time you can check the IP on the session against your logs or something to see who is 'really' active. That's kind of crazy though.
Don't give him idea's!
Any person active on the site within five minutes is perfectly fine.. :).
juangake 08-04-2008, 09:30 AM Trust us :)
As CodyRo said, "Any person active on the site within five minutes is perfectly fine".
You cannot tell who is really active if the underlying connection is TCP. Client connects through http, gets the response from the server and disconnects, right? How can you tell who is REALLY active, if in fact they are disconnecting?
Even if you set a client/server UDP protocol for sessions, you need to implement a "some-minutes-timeout" in order to tell if a conenction has "dropped"...
tchryan 08-04-2008, 09:49 AM Here is some code I have been using for years, yes it uses a flat file on the disk for storing ip addresses but it works - is not session based and is very reliable:
<?php
// check out line 44 for modifying text formatting and the like
// Connection Timeout - between 30 to 60 seconds is the norm
$timer = 60;
// log file - this file tracks the users as they are live on the site
// it is a required file and the webserver must be able to write to it
$filename = "/home/user/browsing.log";
if (!$datei) $datei = dirname(__FILE__)."/$filename";
$time = @time();
$ip = $REMOTE_ADDR;
$string = "$ip|$time\n";
$a = fopen("$filename", "a+");
fputs($a, $string);
fclose($a);
$timeout = time()-(60*$timer);
$all = "";
$i = 0;
$datei = file($filename);
for ($num = 0; $num < count($datei); $num++) {
$pieces = explode("|",$datei[$num]);
if ($pieces[1] > $timeout) {
$all .= $pieces[0];
$all .= ",";
}
$i++;
}
$all = substr($all,0,strlen($all)-1);
$arraypieces = explode(",",$all);
$useronline = count(array_flip(array_flip($arraypieces)));
if ($useronline == 0) {
$useronline = 1;
}
// Output of users online status
print ("<font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b> Browsing Users: </b>$useronline <b>«</b></font>");
$dell = "";
for ($numm = 0; $numm < count($datei); $numm++) {
$tiles = explode("|",$datei[$numm]);
if ($tiles[1] > $timeout) {
$dell .= "$tiles[0]|$tiles[1]";
}
}
if (!$datei) $datei = dirname(__FILE__)."/$filename";
$time = @time();
$ip = $REMOTE_ADDR;
$string = "$dell";
$a = fopen("$filename", "w+");
fputs($a, $string);
fclose($a);
?>
JustinSmall 08-04-2008, 10:30 AM :P Nice :) I had already built the script :( I might give that one a try though.
My site is controlled by time stamps, so I have an ajax box that will refresh every 60 seconds and will read the time stamp for the users. I cannot tell you why it uses time stamps, just that it does :)
|