Jump to content

If statement in foreach loop


meltingpoint

Recommended Posts

Object-  Take array of IP addresses and add up all occurrences of a particular IP address. 

 

 

//$item is an array of IP addresses

foreach ($item as $key => $unique_ip)
					{	
				         if($unique_ip == $ip) 
                                                 $a++;
					}
echo $a;

I get a blank page.  What am I missing?

 

 

Link to comment
Share on other sites

Try var_dump()ing $unique_ip and $ip. Your code looks right. Something must be wrong with your variables.

 

foreach ($item as $key => $unique_ip) {	
echo "I'm comparing this IP:\n";
var_dump($unique_ip);
echo "To this:\n";
var_dump($ip);
if($unique_ip == $ip) $a++;
}
echo $a;

Link to comment
Share on other sites

$ip = $_SERVER['REMOTE_ADDR'];
$a = "";
foreach ($subip as $key => $unique_ip)
{
if($unique_ip == $ip)
$a++;
}
echo $a;

 

This more accurately depicts my script.  Additionally- the purpose is to count the number of times the IP address is in the submission log so that I can stop further submissions.

The code you gave does echo out properly but does not increment $a

Link to comment
Share on other sites

Your code is right. Only thing I can think of is if your stored IPs have extra whitespace or $subip isn't set correctly.

 

Can you post some of the output from the var_dump code I posted.

 

EDIT: Also if you're going to declare $a, you should may want to set it to 0. Not "".

Link to comment
Share on other sites

Hypnos-------Exellent idea.  I added the trim() to the $unique_ip and it worked perfectly!  I also too your advise and made $a set to 0.  The final code that worked is....

$ip = $_SERVER['REMOTE_ADDR'];
$a = "0";
foreach ($subip as $key => $unique_ip)
{
if(trim($unique_ip) == $ip)
$a++;
}
echo $a;

 

Now I can take $a and compare it against X number of submissions.  IF it is over- I will kill the script and ban that IP.

 

Much thanks!

Link to comment
Share on other sites

Problem that I had with array_count_values() is that I want to allow X number of submissions before the script would stop them from submitting again.  I could not figure how to do it with the array_count_values().  To get a one time occurrence I could also have used in_array() to tell me if it was contained in the array itself.

 

If you know of  a way to  test  if "X" number of occurrences of a particular array value have occurred in an array- I would be very interested.

 

 

Link to comment
Share on other sites

This should work. Haven't tested it though.

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$a = 0;
$subip_count = array_count_values($subip);
if(@$subip_count[$ip]) {
$a = $subip_count[$ip];
}
echo $a;

 

Basically array_count_values() is going to give you a massive array like this:

['127.0.0.1'] => 3, ['127.0.0.2'] => 10, etc.

Link to comment
Share on other sites

Hypnos--- inserted your code into my script and it simply echo's- "0".

<?php
$ip = trim($_SERVER['REMOTE_ADDR']);
$subip = array("192.168.1.1","192.168.1.2","192.168.1.3","192.168.1.4");

$a = 0;
$subip_count = array_count_values($subip);
if(@$subip_count[$ip]) 
{
$a = $subip_count[$ip];
}
echo $a;

?>

Not sure how it counts and compares the number of times a particular IP address is in an array?

Link to comment
Share on other sites

Should be right. I just tested it. I put in 192.168.1.1 for $ip and it gave me 1.

 

array_count_values will create a new array with the values and a count for the amount of times they are in the array.

 

Here's an example.

<?php
$subip = array("192.168.1.1","192.168.1.1");

$subip_count = array_count_values($subip);

var_dump($subip_count);

Will give:

array(1) {
  ["192.168.1.1"]=>
  int(2)
}

Because 192.168.1.1 is twice.

 

So when this is called:

echo $subip_count['192.168.1.1'];

It will give "2".

Link to comment
Share on other sites

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.