Jump to content

Is equal to not working for certain varaiable


penisland

Recommended Posts

I have a .txt document named bannedIPS.txt that has this in it:

12.326.25.3
192.168.1.1
207.236.2.32
10.218.24.5

and then my php script has this

//Gets user's IP
$ipaddress = $_SERVER['REMOTE_ADDR'];	

//List of IPs to block
$fh = fopen('UserIP/bannedIPS.txt','a+');
$ipBlockList = array();
$countWhileLoop = 0;
while($line = fgets($fh))
{
	$ipBlockList[$countWhileLoop] = $line;
	$countWhileLoop++;
}

fclose($fh);

//Determine if user IP is on the list
for($i=0; $i<=$countWhileLoop-1; $i++)
{ 

//Debug for seeing what $ipBlockList[$i] is
echo $ipBlockList[$i]."<br/>";

	if($ipaddress==$ipBlockList[$i])
	{
		echo "banned";
		exit;
	}
}

My IP address is 207.236.2.32 so it should display:

 

12.326.25.3
192.168.1.1
207.236.2.32

banned

 

but it displays:

 

12.326.25.3
192.168.1.1
207.236.2.32
10.218.24.5

 

so, the if statement isn't getting triggered. Why is that?

 

I'v tried:

if($ipaddress=="207.236.2.32")
	{
		echo "banned";
		exit;
	}

and then it works, but if it's the variable, it doesn't.

 

PS. I just started learning PHP today from W3Schools and Google.

 

Edit: Fixed typo

i am not seeing a problem with your expected outcome. i'd do it a little different but that's normal, long story short i copy/pasted/uploaded and added my ip to bannedips.txt, it it prints:

 

12.326.25.3 
192.168.1.1 
207.236.2.32 
10.218.24.5 
xx.xx.xx.xx (my ip)
banned

 

 

sooo... yeah.

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.