TINNY Posted December 16, 2004 Author Report Posted December 16, 2004 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. Quote
alexander Posted December 16, 2004 Report Posted December 16, 2004 Originally Posted by swirleeThe 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 Quote
TINNY Posted December 16, 2004 Author Report Posted December 16, 2004 you guys are so good at finding information on the net. i gotta improve those skills a lot Quote
alexander Posted December 16, 2004 Report Posted December 16, 2004 Well, there's always google, but you gotta know how to search, because two nearly identical requests will give you different results, but dont worry, you'll get better at it with time! Quote
TINNY Posted December 16, 2004 Author Report Posted December 16, 2004 i'm trying to learn as much as i can during my month-long tenure as web programmer and designer right now where i have a computer to work with at all times with broadband connection. my time will be up when the new sem kicks in in Jan. Quote
TINNY Posted December 16, 2004 Author Report Posted December 16, 2004 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? Quote
TINNY Posted December 18, 2004 Author Report Posted December 18, 2004 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.... Quote
TINNY Posted December 18, 2004 Author Report Posted December 18, 2004 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 Quote
Tormod Posted December 18, 2004 Report Posted December 18, 2004 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. Quote
Tormod Posted December 18, 2004 Report Posted December 18, 2004 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 $_SERVER['REMOTE_ADDR'] Quote
TINNY Posted December 18, 2004 Author Report Posted December 18, 2004 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. Quote
Tormod Posted December 18, 2004 Report Posted December 18, 2004 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". :) Quote
Tormod Posted December 18, 2004 Report Posted December 18, 2004 I see you're opting for an IP tracker rather than a cookie tracker. I think that's a much harder way to go. Quote
TINNY Posted December 18, 2004 Author Report Posted December 18, 2004 i already asked you how to do the cookie cracker which you did not reply. so i stuck with the ip tracker. Quote
TINNY Posted December 18, 2004 Author Report Posted December 18, 2004 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. Quote
Tormod Posted December 18, 2004 Report Posted December 18, 2004 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.