wtfsmd Posted September 21, 2008 Share Posted September 21, 2008 I am trying to keep track of visitors by storing their IP and and the last active time that they visited. my script works ... but sometimes i get duplicates when all i want is a single record for them and it just updates the last time they visited (the reason i have it update instead of inserting a new row is because i have the $_SESSION['last_active_date'] update every time they reload or navigate to a new page so i can see if they are active or not.) anyways .. here is the code: $query = "SELECT * FROM guest"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ $stored_ip = $row['ip']; } $ip = $_SERVER['REMOTE_ADDR']; if($stored_ip == $ip){ mysql_query("UPDATE guest SET username='$gname', last_active_date='$date', last_active_time='$thetime', browser='$browser' WHERE ip='$ip'") or die(mysql_error()); }else{ mysql_query ("INSERT INTO guest (username, ip, last_active_date, last_active_time, browser) VALUES ('$gname', '$ip', '$date', '$thetime', '$browser')") or die(mysql_error()); } Link to comment https://forums.phpfreaks.com/topic/125149-keeping-track-of-visitors-using-their-ip-addresses-phpmysql/ Share on other sites More sharing options...
redarrow Posted September 21, 2008 Share Posted September 21, 2008 wast off time use the users id.... sorry but ip address change to much........... Link to comment https://forums.phpfreaks.com/topic/125149-keeping-track-of-visitors-using-their-ip-addresses-phpmysql/#findComment-646841 Share on other sites More sharing options...
wtfsmd Posted September 21, 2008 Author Share Posted September 21, 2008 this is to track visitors, if they logged in it wouldn't be a problem just wanted to track the people who visit the site. you cant track their user name if they don't have one. Link to comment https://forums.phpfreaks.com/topic/125149-keeping-track-of-visitors-using-their-ip-addresses-phpmysql/#findComment-646843 Share on other sites More sharing options...
redarrow Posted September 21, 2008 Share Posted September 21, 2008 agree sorry............. my fault............ Link to comment https://forums.phpfreaks.com/topic/125149-keeping-track-of-visitors-using-their-ip-addresses-phpmysql/#findComment-646844 Share on other sites More sharing options...
redarrow Posted September 21, 2008 Share Posted September 21, 2008 example only do your thing ....... <?php //database connection $ip=$_SERVER['REMOTE_ADDR']; $sql="select from sec where ip='$ip'"; $res=mysql_query($sql)or die(mysql_error()); if(mysql_num_rowa(res)==1){ $sql2="update sec set ip='$ip' where ip='$ip'"; $res2=mysql_query($sql)or die(mysql_error()); }else{ $sql3="inset into sec(ip)VALUES($ip)"; $res3=mysql_query($sql)or die(mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/125149-keeping-track-of-visitors-using-their-ip-addresses-phpmysql/#findComment-646846 Share on other sites More sharing options...
F1Fan Posted September 21, 2008 Share Posted September 21, 2008 This would be much more efficient: <?php $ip = $_SERVER['REMOTE_ADDR']; $result = mysql_query("UPDATE guest SET username='$gname', last_active_date='$date', last_active_time='$thetime', browser='$browser' WHERE ip='$ip'") or die(mysql_error()); if (mysql_affected_rows($result)==0){ mysql_query ("INSERT INTO guest (username, ip, last_active_date, last_active_time, browser) VALUES ('$gname', '$ip', '$date', '$thetime', '$browser')") or die(mysql_error()); } ?> That would try to update first. Then, if it affected zero rows, it would insert. Otherwise it wouldn't have to do anything else. Best yet, you wouldn't have to run you (potentially) huge SELECT query. Link to comment https://forums.phpfreaks.com/topic/125149-keeping-track-of-visitors-using-their-ip-addresses-phpmysql/#findComment-646847 Share on other sites More sharing options...
wtfsmd Posted September 21, 2008 Author Share Posted September 21, 2008 This would be much more efficient: <?php $ip = $_SERVER['REMOTE_ADDR']; $result = mysql_query("UPDATE guest SET username='$gname', last_active_date='$date', last_active_time='$thetime', browser='$browser' WHERE ip='$ip'") or die(mysql_error()); if (mysql_affected_rows($result)==0){ mysql_query ("INSERT INTO guest (username, ip, last_active_date, last_active_time, browser) VALUES ('$gname', '$ip', '$date', '$thetime', '$browser')") or die(mysql_error()); } ?> That would try to update first. Then, if it affected zero rows, it would insert. Otherwise it wouldn't have to do anything else. Best yet, you wouldn't have to run you (potentially) huge SELECT query. This gives off an error: Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /home/content/t/i/m/timeba/html/globals.php on line 57 Link to comment https://forums.phpfreaks.com/topic/125149-keeping-track-of-visitors-using-their-ip-addresses-phpmysql/#findComment-646851 Share on other sites More sharing options...
F1Fan Posted September 21, 2008 Share Posted September 21, 2008 Sorry, I thought the syntax of that function was the same as another I use. Try this: <?php $ip = $_SERVER['REMOTE_ADDR']; mysql_query("UPDATE guest SET username='$gname', last_active_date='$date', last_active_time='$thetime', browser='$browser' WHERE ip='$ip'") or die(mysql_error()); if (mysql_affected_rows()==0){ mysql_query ("INSERT INTO guest (username, ip, last_active_date, last_active_time, browser) VALUES ('$gname', '$ip', '$date', '$thetime', '$browser')") or die(mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/125149-keeping-track-of-visitors-using-their-ip-addresses-phpmysql/#findComment-646853 Share on other sites More sharing options...
redarrow Posted September 21, 2008 Share Posted September 21, 2008 what about this way mate! <?php //database connection $ip=$_SERVER['REMOTE_ADDR']; $sql="select from sec where ip='$ip'"; $res=mysql_query($sql)or die(mysql_error()); if(mysql_num_rowa(res)==1){ $sql2="update sec set ip='$ip' where ip='$ip'"; $res2=mysql_query($sql)or die(mysql_error()); }else{ $sql3="inset into sec(ip)VALUES($ip)"; $res3=mysql_query($sql)or die(mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/125149-keeping-track-of-visitors-using-their-ip-addresses-phpmysql/#findComment-646854 Share on other sites More sharing options...
wtfsmd Posted September 21, 2008 Author Share Posted September 21, 2008 i used: <?php $ip=$_SERVER['REMOTE_ADDR']; $sql="SELECT * FROM guest WHERE ip='$ip'"; $res=mysql_query($sql)or die(mysql_error()); if(mysql_num_rows(res)==1){ $sql2="UPDATE guest SET ip='$ip' WHERE ip='$ip'"; $res2=mysql_query($sql)or die(mysql_error()); }else{ $sql3="INSERT INTO guest (ip)VALUES($ip)"; $res3=mysql_query($sql)or die(mysql_error()); } ?> And i get the error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/t/i/m/timeba/html/globals.php on line 58 Link to comment https://forums.phpfreaks.com/topic/125149-keeping-track-of-visitors-using-their-ip-addresses-phpmysql/#findComment-646863 Share on other sites More sharing options...
redarrow Posted September 21, 2008 Share Posted September 21, 2008 <?php $ip=$_SERVER['REMOTE_ADDR']; $sql="SELECT * FROM guest WHERE ip='$ip'"; $res=mysql_query($sql)or die(mysql_error()); if(mysql_num_rows($res)==1){ $sql2="UPDATE guest SET ip='$ip' WHERE ip='$ip'"; $res2=mysql_query($sql2)or die(mysql_error()); }else{ $sql3="INSERT INTO guest (ip)VALUES($ip)"; $res3=mysql_query($sql3)or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/125149-keeping-track-of-visitors-using-their-ip-addresses-phpmysql/#findComment-646868 Share on other sites More sharing options...
F1Fan Posted September 21, 2008 Share Posted September 21, 2008 You're missing the '$' in front of 'res' Link to comment https://forums.phpfreaks.com/topic/125149-keeping-track-of-visitors-using-their-ip-addresses-phpmysql/#findComment-646869 Share on other sites More sharing options...
redarrow Posted September 21, 2008 Share Posted September 21, 2008 i no sorry lol..... you might want NULL here i think $sql2="UPDATE guest SET ip=NULL WHERE ip='$ip'"; <?php $ip=$_SERVER['REMOTE_ADDR']; $sql="SELECT * FROM guest WHERE ip='$ip'"; $res=mysql_query($sql)or die(mysql_error()); if(mysql_num_rows($res)==1){ $sql2="UPDATE guest SET ip='$ip' WHERE ip='$ip'"; $res2=mysql_query($sql2)or die(mysql_error()); }else{ $sql3="INSERT INTO guest (ip)VALUES($ip)"; $res3=mysql_query($sql3)or die(mysql_error()); ?> Link to comment https://forums.phpfreaks.com/topic/125149-keeping-track-of-visitors-using-their-ip-addresses-phpmysql/#findComment-646871 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.