shortysbest Posted July 2, 2010 Share Posted July 2, 2010 I am working on unique page view counter however i am running into this problem with my code. What i am trying to do is to create a unique view count per page and i have been attempting to do so a couple of ways. the current way i am doing it gets the users ip and gets the page they are on and inserts it into a single column in the database. table name is ips, column name is ip. inside of the ip column if the user is on the homepage it should insert a value that looks similar to this: 67.220.153.249 home or if they were on a page called shows it would look like this: 67.220.153.249 shows however with the code below once there is a line in it, it adds 67.220.153.249 home everytime i click home, not just once. but i have the code saying if the database row ip is not equal to the current users ip and page to add the row in the database else it should not add a row. I have been changing the code all around for awhile and now i just cant figure it out. <?php //get users ip address//start// if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet { $ip=$_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy { $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip=$_SERVER['REMOTE_ADDR']; } //get users ip address//end// $pg = $_GET['node']; $ippage = $ip.' '.$pg; $ipp = mysql_query("SELECT * FROM ips WHERE ip='$ip'"); $ipf = mysql_fetch_assoc($ipp); if ($ipf['id']!==$ippage) $query = mysql_query("INSERT INTO ips SET ip='$ippage'"); ?> Link to comment https://forums.phpfreaks.com/topic/206492-insert-ip-into-database-if-it-doesnt-exist-problem/ Share on other sites More sharing options...
fred12ned Posted July 3, 2010 Share Posted July 3, 2010 $ipp = mysql_query("SELECT * FROM ips WHERE ip='$ip'"); $ipf = mysql_fetch_assoc($ipp); if ($ipf['id']!==$ippage) $query = mysql_query("INSERT INTO ips SET ip='$ippage'"); to $ipp = mysql_query("SELECT * FROM ips WHERE ip='$ippage'"); $ipnum = mysql_num_rows($ipp); if (!$ipnum) $query = mysql_query("INSERT INTO ips VALUES('$ippage')"); Link to comment https://forums.phpfreaks.com/topic/206492-insert-ip-into-database-if-it-doesnt-exist-problem/#findComment-1080636 Share on other sites More sharing options...
shortysbest Posted July 3, 2010 Author Share Posted July 3, 2010 $ipp = mysql_query("SELECT * FROM ips WHERE ip='$ip'"); $ipf = mysql_fetch_assoc($ipp); if ($ipf['id']!==$ippage) $query = mysql_query("INSERT INTO ips SET ip='$ippage'"); to $ipp = mysql_query("SELECT * FROM ips WHERE ip='$ippage'"); $ipnum = mysql_num_rows($ipp); if (!$ipnum) $query = mysql_query("INSERT INTO ips VALUES('$ippage')"); Thanks for the reply, I never thought about doing it by num_rows, but i did get everything working now doing it a little differently. But thank you. I did it: if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet { $ip=$_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy { $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip=$_SERVER['REMOTE_ADDR']; } $pg = $_GET['node']; if ($_GET['node']=="product"){ $itm = $_GET['id']; $ippage = $ip.' '.$pg.' '.$itm; } else $ippage = $ip.' '.$pg; $ipp = mysql_query("SELECT * FROM ips WHERE ip='$ippage'"); $ipf = mysql_fetch_assoc($ipp); if ($ipf['ip']==$ippage) {} else mysql_query("INSERT INTO ips SET ip='$ippage'"); Link to comment https://forums.phpfreaks.com/topic/206492-insert-ip-into-database-if-it-doesnt-exist-problem/#findComment-1080735 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.