L Posted July 15, 2007 Share Posted July 15, 2007 Hey, I have come to a wall. I want to add, count, and remove guests from the active_guests table. So when a guest who is not logged in comes to the site he will be added to the table, but when he leaves or logs in he gets deleted from the table. But then i count the rows and echo it. The part i need help is with adding and deleting guests when they come and leave the site. I'm not sure how to do this, but in my table i have an ip row where it eneters their ip to track them by, can anyone point me in the right direction Also I have another problem(instead of making another topic) ill post it here too. I want to count how many new pms a user has, so i did this... $pmnum = mysql_query("SELECT `to_id`, `new` * FROM `PMs` WHERE `to_id`='".$_SESSION['userid']."' and `new`='0'"); $num = mysql_numrows($pmnum); and then i would echo it out....but im getting nothing..not even a zero...can someone help me with this too? Thank you for your time, ~L Quote Link to comment Share on other sites More sharing options...
infid3l Posted July 15, 2007 Share Posted July 15, 2007 Try changing: $num = mysql_numrows($pmnum); to $num = mysql_num_rows($pmnum); Quote Link to comment Share on other sites More sharing options...
L Posted July 15, 2007 Author Share Posted July 15, 2007 hmm...that doesn't seem to make a difference, nothing is still showing......i tried to use COUNT() on the query but that always gave me a 1, even though i have more than 1 new pm( or more than one pm who's new value is 0(meaning its new)) Quote Link to comment Share on other sites More sharing options...
L Posted July 15, 2007 Author Share Posted July 15, 2007 Sweet....i got the counting new pms to work by putting require("database.php"); $pm = mysql_query("SELECT * FROM `PMs` WHERE `to_id`='".$_SESSION['userid']."'") or die(mysql_error()); $numpm = mysql_num_rows($pm); it seems that it wasn;t connecting to the database because when i required database.php it was in another if, so it mustn't have worked....but i still have no idea how to do the guest thing...can anyone help me? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 15, 2007 Share Posted July 15, 2007 You can put the "unknown" user who comes to your site into the database, and store their IP...but you will never be able to tell if they logged in as there is no way to tell which user they were. You COULD TRY telling who they were by their IP, but IP addresses change all the time, so that wouldn't work too well. I'm not sure if what your trying to achieve is possible. Quote Link to comment Share on other sites More sharing options...
marcus Posted July 15, 2007 Share Posted July 15, 2007 You could do something like, updating a time column for each time they navigate around the site. And if there is no activity within 5 minutes, delete them from the table. Quote Link to comment Share on other sites More sharing options...
L Posted July 15, 2007 Author Share Posted July 15, 2007 mgallforever, your solution seems like it can work...and pocobueno, maybe i didn't explain myself correctly, i meant delete a person that's in the table if they are not active, which would mean they either left, or logged in....i just couldn't really find the word, but being inactive for 5 minutes is something that seems reasonable. So with the method: would i have an insert and update query on everypage so i can grab the guests timestamp?(insert for ip, and time, and then update for the time)? and also how would i have the if for every five minutes? Thanx for your help ~L Quote Link to comment Share on other sites More sharing options...
trq Posted July 15, 2007 Share Posted July 15, 2007 would i have an insert and update query on everypage so i can grab the guests timestamp?(insert for ip, and time, and then update for the time)? Yes. how would i have the if for every five minutes? The best way would be to setup a cron job to execute the given query every 5 minutes. If you don't have access to cron though you could also run the query on each request. Quote Link to comment Share on other sites More sharing options...
L Posted July 15, 2007 Author Share Posted July 15, 2007 yah, i don't have access to cron, unfortunately but ill just have t so if goes everytime the page is refreshed, since its at the top of the page. but the problem im having is that when im logged out im not being inserted into the database...here's my code <? session_start(); $time = time(); $ip = $_SERVER['REMOTE_ADDR']; if (empty($_SESSION['userid']) || empty($_SESSION['username'])) { $row = mysql_fetch_array(mysql_query("SELECT * FROM `active_guests` WHERE `ip`='$ip'")); if ($row['ip'] == $ip) { mysql_query("UPDATE `active_guests` SET `timestamp`='$time'"); } else { mysql_query("INSERT INTO `active_guests` (`ip`,`timestamp`) VALUES('".$ip."','".$time."')"); } } ?> This is the include at the top of all pages EDIT NeverMind, I forgot to make a connection Quote Link to comment Share on other sites More sharing options...
L Posted July 15, 2007 Author Share Posted July 15, 2007 ok...now that i added a guest..how would i make it so the database deletes him if he's inactive for 5 minutes(meaning he left the website or logged in) Quote Link to comment Share on other sites More sharing options...
L Posted July 15, 2007 Author Share Posted July 15, 2007 ok...now that i added a guest..how would i make it so the database deletes him if he's inactive for 5 minutes(meaning he left the website or logged in) anyone? i really can't think of anything here Quote Link to comment Share on other sites More sharing options...
L Posted July 15, 2007 Author Share Posted July 15, 2007 ok...what's wrong with this code....it seems like it should work, but it's not deleting me <? session_start(); $time = time(); include("database.php"); $ip = $_SERVER['REMOTE_ADDR']; if (empty($_SESSION['userid']) || empty($_SESSION['username'])) { $row = mysql_fetch_array(mysql_query("SELECT * FROM `active_guests` WHERE `ip`='$ip'")); if ($row['ip'] == $ip) { mysql_query("UPDATE `active_guests` SET `timestamp`='$time'"); } else { mysql_query("INSERT INTO `active_guests` (`ip`,`timestamp`) VALUES('".$ip."','".$time."')"); } } mysql_query("CREATE EVENT updateguests ON SCHEDULE EVERY 1 MINUTE DO DELETE FROM 5810_riaz.active_guests WHERE timestamp < (UNIX_TIMESTAMP()-300)"); ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted July 15, 2007 Share Posted July 15, 2007 do u mean this dont get your code sorry <?php session_start(); include("database.php"); $time = time(); $ip = $_SERVER['REMOTE_ADDR']; if (empty($_SESSION['userid']) || empty($_SESSION['username'])) { $query="SELECT * FROM `active_guests` WHERE `ip`='$ip'"; $result=mysql_query($query)or die("mysql_error"); while($row = mysql_fetch_assoc($result)){ if ($row['ip'] == $ip) { $quert2="UPDATE `active_guests` SET `timestamp`='$time'"; $result2=mysql_query($query2)or die("mysql_error"); } else { $query3="INSERT INTO `active_guests` (`ip`,`timestamp`) VALUES('".$ip."','".$time."')"; $result3=mysql_query($query2)or die("mysql_error"); $query4="DELETE FROM 5810_riaz.active_guests WHERE timestamp < (UNIX_TIMESTAMP()-300)"; $resilt4=mysql_query($query4)or die("mysql_error"); } } } ?> Quote Link to comment Share on other sites More sharing options...
L Posted July 15, 2007 Author Share Posted July 15, 2007 hmmm....that seems right, but i want it to delete the user in active_guests if he leaves the site...so if `timestamp` is more than five minutes of $time, it deletes the dude from the table.... EXAMPLE: I go to my site...it inputs me in the guest table....but if i leave the site or log in(meaning the guest is now currently inactive) it deletes that ip from the table.....so to see if a user is active or not the query runs whenever the page is loaded, and if the time in the table that was inserted when i came to the site is more than five minutes off then it deletes the user, because that means the guest left, logged in, or is not browsing my site anymore.... If that still doesn't make sense, then i ask this question. How do I count how many guests are viewing my site? <- that's my purpose of all of this, counting how many users are viewing the site. Quote Link to comment Share on other sites More sharing options...
L Posted July 15, 2007 Author Share Posted July 15, 2007 I really don't know why it's not letting me edit my post...but here is my edit i want something like this 343 user(s) are online Members: 4 Guests: 339 I got the members part down...i just need to know how to do the guests part 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.