Jump to content
Science Forums

'Users Online' PHP script


TINNY

Recommended Posts

  • Replies 67
  • Created
  • Last Reply

Top Posters In This Topic

So the script you see on this site uses cookies to track users, combined with a database table for each cookie. Each user only gets one cookie and it is the availability of this cookie that tells the script whether the user is online or not. If the cookie has not been read for 15 minutes, then the user is considered offline.
how does that exactly work? i can't figure it out.
Link to comment
Share on other sites

Originally Posted by swirlee

The trouble is there's no "disconnection" to catch. As soon as your browser finishes loading the page, it is disconnected. Right now, as I'm typing this into the phpBB box, I'm not connected to the server, and as you're reading it, you're not connected to the server, either (unless it's still loading, I suppose).

 

That's where sessions come in. When you see a forum that says "15 users online", all it really means is that 15 users have visited (i.e. loaded a page) inthe last X minutes. None of them are actually "connected", unless they're in the middle of loading a page.

 

That's essentially how sessions work, too -- if the session has been accessed in the last X minutes, it remains active; if it hasn't been, it's removed.

 

There is no way you can know whether the user has hit the stop button or closed the browser, and if you think about it...

 

i think that just about explains how you use sessions and databases...

I'd recommend reading up on sessions: http://www.php.net/manual/en/ref.session.php

Link to comment
Share on other sites

hey you guys. there's something wrong with the script. it somehow misses to delete some records that have been timed out. need immediate help before my boss notices something wrong with the website.!! here's the script again.

<?php

 

$server = "xxx";

$db_user = "xxx";

$db_pass = "xxx";

$database = "xxx";

$timeoutseconds = 300;

 

$timestamp = time();

$timeout = $timestamp-$timeoutseconds;

 

mysql_connect($server, $db_user, $db_pass);

$insert = mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')");

 

if(!($insert)) {

print "Useronline Insert Failed > ";

}

 

$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");

 

if(!($delete)) {

print "Useronline Delete Failed > ";

}

 

$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'");

 

if(!($result)) {

print "Useronline Select Error > ";

}

 

$user = mysql_num_rows($result);

 

mysql_close();

 

if($user == 1) {

print("<b>$user</b> user onlinen");

} else {

print("<b>$user</b> users onlinen");

}

 

?>

something's wrong in the way it deletes timed out records.

 

one more, what's the use of the $PHP_SELF variable?

Link to comment
Share on other sites

i notice that the users count decreases everyday. it only works on the first day 'properly'. by the way, i set the $timeoutseconds = 10800 (which is three hours). does that in any way cause the failure? another problem is, i don't understand how to read the time() data. i can't decipher the format of the time.I REALLY need to know how to read it so I can analyze what is going on. does it take into account the date as well?

 

alex, please....

Link to comment
Share on other sites

mysql_connect($server, $db_user, $db_pass);

$insert = mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')");

if i were to change the input from '$REMOTE_ADDR' as alex suggested, is it '$_SERVER[REMOTE_ADDR]' or $_SERVER['REMOTE_ADDR']. i'm not sure where to put the apostophe.

thanks

Link to comment
Share on other sites

i notice that the users count decreases everyday. it only works on the first day 'properly'. by the way, i set the $timeoutseconds = 10800 (which is three hours).

I'll see if I can look at your script - but 3 hours is WAY too much. NO users will stay on your site for that amount of time. 15 minutes is plenty - 900 seconds.

Link to comment
Share on other sites

i'm using three hours to increase the number because, as i said before, i couldn't get many different IPs. when i use three hours, i get something of an accurate number based on my webalizer software even though the calculation is not correct.

Well, you get a NUMBER allright but it's not the correct number. If you're looking for a "Who's Online" thingy you need a much shorter timeout. What you get now is an "X users were online in the past 3 hours". :)

Link to comment
Share on other sites

Well, you get a NUMBER allright but it's not the correct number. If you're looking for a "Who's Online" thingy you need a much shorter timeout. What you get now is an "X users were online in the past 3 hours".
what i mean is that based on calculations of data from webalizer, i get about 3000 visitors per day. this equates to about 21 per 10 minutes. but the php script gives me a number around 5 (and that is TOTAL rows, not distinct IPs). for distinct IPs, i get about 1 or 2 only. so i adjust the timeout to get a number around 20.
Link to comment
Share on other sites

what i mean is that based on calculations of data from webalizer, i get about 3000 visitors per day. this equates to about 21 per 10 minutes. but the php script gives me a number around 5 (and that is TOTAL rows, not distinct IPs). for distinct IPs, i get about 1 or 2 only. so i adjust the timeout to get a number around 20.

You should ask yourself which set of results are more likely to be correct. Fixing the data like that is not good. You better find out if a) the data is wrong or :) your way of getting the data is wrong.

 

Also ask yourself whether 3,000 visitors per day is correct or not. It sounds like a lot to me. It his a busy site? Hypography is not an extremely busy site but we have around 6,000-8,000 visitors per day on average.

 

Do you get 1 or 2 distinct IP's per 10 minutes? That sounds more reasonable, depending on what site you are working on.

 

Comparing tracking scripts with log files is not easy. The log file contains every single hit, including images. So on average I'd say that one unique IP in the log file will average somewhere between 10-15 hits per page. And the log file also includes every single page they view...so the number you're looking for is more likely to be distinct ip / number of pages viewed.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...