Jump to content

Is equal to not working for certain varaiable


Go to solution Solved by Barand,

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

Edited by penisland

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.

  • Solution

 


Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value)

 

so you need to trim the lines from the file

while($line = trim(fgets($fh)))
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.