pneudralics Posted November 17, 2008 Share Posted November 17, 2008 One of my page records ips into the database. When the ip has been recorded more than 5 times I want to allow it to other pages. I can get the current ip to compare to the database ip, once it finds 5 ips in the database I want it redirect it. $ip = $_SERVER['REMOTE_ADDR']; //Select and Retrieve ip $ipselect = "SELECT * FROM ip"; //Counts ips $ipcountresult = mysql_query($ipselect); // Get number of articles, assign value to variable $ipcount = mysql_num_rows($ipcountresult); if ($ipresult = mysql_query ($ipselect)) { while ($row = mysql_fetch_array ($ipresult)) { //Look for ip in database $ip2 = $row['ip']; } } //CAN'T SEEM TO GET THIS PART TO WORK if ($ip = $ip2 and has more than 5 $ip in database) { do this } Quote Link to comment https://forums.phpfreaks.com/topic/133123-compare-ips-with-amount-in-database/ Share on other sites More sharing options...
ratcateme Posted November 17, 2008 Share Posted November 17, 2008 why are you doing this with IP's i think cookies would be a much more specific thing as now days there are multiple people behind one IP and they are ever changing. where cookies would be user specific. with a cookie you could record the view count in the cookie and no need for a database. this is a very easily hacked cookie to make it say 5 but its not to much easier than going F5 5 times for the redirect have a look at the header function header() Scott. Quote Link to comment https://forums.phpfreaks.com/topic/133123-compare-ips-with-amount-in-database/#findComment-692305 Share on other sites More sharing options...
pneudralics Posted November 17, 2008 Author Share Posted November 17, 2008 It's a login for admin section so I want to block ips after 5 failed attempts and redirect it to an error page. Quote Link to comment https://forums.phpfreaks.com/topic/133123-compare-ips-with-amount-in-database/#findComment-692368 Share on other sites More sharing options...
ratcateme Posted November 17, 2008 Share Posted November 17, 2008 try this <?php $ip = $_SERVER['REMOTE_ADDR']; //Select and Retrieve ip $query = "SELECT * FROM ip WHERE `ip` = '{$ip}'"; //Counts ips $result = mysql_query($query); // Get number of articles, assign value to variable $count = mysql_num_rows($result); if ($count == 0) {//first try $query = "INSERT INTO `ip`(`ip`,`count`) VALUES ('{$ip}',1)"; mysql_query($query); }else{ $try = mysql_result($result,0,"count"); if($try >= 5){ header("Loaction: error.php"); }else{ $query = "UPDATE `ip` SET `count` = `count` + 1 WHERE `ip` = '{$ip}'"; mysql_query($query); } } it requires a database with 2 fields count and ip i would recommend adding a time field and a cron job to remove entries over say 12 hours old. Scott. Quote Link to comment https://forums.phpfreaks.com/topic/133123-compare-ips-with-amount-in-database/#findComment-692378 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.