phpSensei Posted August 24, 2007 Share Posted August 24, 2007 I modified it a bit from RedArrow, and it seems to work. I don't know what I changed but I just fixed the errors, and it actually echos the number of guests online. Anything Wrong? <?php function online_users($period = 300 /* seconds */) { $query = "DELETE FROM `online_users` WHERE `updated` < '".(($now = time()) - $period)."'"; $result = mysql_query($query) or die ($query.' -- '.mysql_error()); $query = "INSERT INTO `online_users` (`ip`, `updated`, `user`) ". "VALUES ('".$_SERVER['REMOTE_ADDR']."', '".$now."', '".(isset($_COOKIE['user'])?1:0)."')". "ON DUPLICATE KEY UPDATE `updated`='$now', `user` = '".(isset($_COOKIE['user'])?1:0)."'"; mysql_query($query) or die ($query.' -- '.mysql_error()); $query = "SELECT * FROM `online_users`"; $result = mysql_query($query) or die ($query.' -- '.mysql_error()); $return = array('users' => 0, 'guests' => 0); if(mysql_num_rows($result) > 0) { while($row = mysql_fetch_assoc($result)) { if($row['user'] == 1) { $return['users']++; } else { $return['guests']++; } } } return $return; } ?> <?php # in use $online = online_users(); $s['users'] = ($online['users'] != 1) ? 's' : NULL; $s['guests'] = ($online['guests'] != 1) ? 's' : NULL; #print results echo $online['guests'].$s['guests']; ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 24, 2007 Share Posted August 24, 2007 And the problem is? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 24, 2007 Author Share Posted August 24, 2007 Thats the problem, I don't even know if it works. It does Echo "1", but what if its just the number "1" even with thousands of people online on your site... I was wondering if you inspect the code of something, to see if its valid. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 24, 2007 Share Posted August 24, 2007 It would be far easier to get someone else to test it with you! After all, you'll never be 100% sure it is working until it is fully tested. Quote Link to comment Share on other sites More sharing options...
Neptunus Maris Posted August 24, 2007 Share Posted August 24, 2007 You should do: $query = "INSERT INTO `online_users` (`ip`, `updated`, `user`) ". "VALUES ('".$_SERVER['REMOTE_ADDR']."', '".$now."', '".(isset($_COOKIE['user'])?1:0)."')". "ON DUPLICATE KEY UPDATE `updated`='$now', `user` = '".(isset($_COOKIE['user'])?1:0)."'"; mysql_query($query) or die ($query.' -- '.mysql_error()); on the login page anyways...thats just my opinion Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 24, 2007 Author Share Posted August 24, 2007 Havnt Gotten That far in the forum, I will when I finish user registration. It Works BTW, It Goes To Number 2 when someone else goes on it, and three minutes later, it goes back to 1 , because he left the site. Quote Link to comment Share on other sites More sharing options...
Neptunus Maris Posted August 24, 2007 Share Posted August 24, 2007 Or just simply have a new field for the data table `online` bigint(12) unsigned NOT NULL default '0' then as a user logs in, put this on the login page after you write a code that grabs that user's ID: $now = time() $query = "UPDATE users SET online = '$now' WHERE userID = '$userID'"; $result = ...bla bla --------------------------------------------------------------------- then on the whatever page...to count how many users are online: $now = time(); $last_five = time() - 300; $query = "SELECT * FROM users WHERE online > '$last_five' AND online < '$now'"; $result = ...bla ..bla That should work Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 24, 2007 Author Share Posted August 24, 2007 Hey, I appreciate that, Will use it as a reference. I Re-Wrote The Script, And OMG, it went from 30 lines to only 12. The Script Written by red arrow was too long. Quote Link to comment Share on other sites More sharing options...
Neptunus Maris Posted August 24, 2007 Share Posted August 24, 2007 But does it work? cause i threw that up out of nowhere Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 24, 2007 Author Share Posted August 24, 2007 It Makes Some Sense, But I Will UnixTimeStamp instead of time(); Quote Link to comment Share on other sites More sharing options...
Neptunus Maris Posted August 24, 2007 Share Posted August 24, 2007 time(); is a unix time stamp http://www.php.net/manual/en/function.time.php Quote Link to comment Share on other sites More sharing options...
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.