Jump to content

compare ips with amount in database


pneudralics

Recommended Posts

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
}

Link to comment
https://forums.phpfreaks.com/topic/133123-compare-ips-with-amount-in-database/
Share on other sites

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.