Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.