Page 1 of 2
Comparing the visitors IP, with the IP's in a file
Posted: Tue Oct 08, 2002 6:25 am
by GoodspeeÐ
Hi there,
I have made up a textfile, called banfile.txt in which I've put the IP's of users, which I do not want to perform a certain action. To see if the users IP is in the file banfile.txt I made a function, called isbanned(), that returns either a 1 if the user is banned or a 0 if he's not.
This is de code for that function.
Code: Select all
function isbanned() {
$banfile = "banfile.txt";
$localip = $_SERVERї'REMOTE_ADDR'];
if (!file_exists("$banfile")) {
$bfp = fopen("$banfile", "a");
fclose($bfp);
}
$bfp = fopen("$banfile","r") or die ("error when opening banfile.txt");
$buffer = fgets($bfp);
while(!feof($bfp) && $buffer != $localip) {
$buffer = fgets($bfp);
}
fclose($bfp);
if ($buffer == $localip) {
return 1;
}
else {
return 0;
}
}
The problem is, that while executing, the script doesn't leave the while statement and I get an error after 10 seconds, because it takes to long. I really can't see why....
Is there anyone that know this problem, or sees an error in my code? Please let me know.
Greetings,
Tonni Tielens
Posted: Tue Oct 08, 2002 7:12 am
by Coco
well for one thing, with teh and statement you are using, even if the $buffer does equal $localip, the while will continue executing... (since to end the while, buffer has to be localip AND you have to be at eof)
try this:
Code: Select all
while(!feof($bfp)) {
$buffer = fgets($bfp);
if($buffer == $localip)
return 1;
}
return 0;
Posted: Tue Oct 08, 2002 7:24 am
by Wayne
why dont you just use the file() command (this will create an array object per line in the file, so put 1 ip per line) to read the file into an array then use the in_array() function to see if the IP exists in the array.
then you dont have to worry about the EOF etc.
Posted: Tue Oct 08, 2002 7:48 am
by GoodspeeÐ
Already solved it with the file() and in_array() functions.
(since to end the while, buffer has to be localip AND you have to be at eof)
That's not true CoCo.
The while statements continues as longs as buffer is not localip AND I'm not at eof! So if buffer is localip OR if I'm at eof, the while statement should stop.
Posted: Tue Oct 08, 2002 9:16 am
by mikeq
GoodspeeÐ wrote:
That's not true CoCo.
The while statements continues as longs as buffer is not localip AND I'm not at eof! So if buffer is localip OR if I'm at eof, the while statement should stop.
have you just answered your own logic there, should it not be OR instead of AND. So if $buffer != $localip and you reach the end of the file the while loop ends? what happens if $buffer == $localip? do both conditions not need to evaluate to true to quit the while loop?
Posted: Tue Oct 08, 2002 10:25 am
by GoodspeeÐ
Guys.... the name while says it all.
The while loop doesn't stop when the condition is true, but it goes as long as the condition is true.
if it said something like DO UNTIL, then you would be right... but not this time.

Posted: Tue Oct 08, 2002 12:12 pm
by Coco
(!feof($bfp) && $buffer != $localip) is always true, unless feof!=false(true) AND buffer=localip
therefore your while loop will carry on to spin round and round until you reach EOF AND buffer=localip
Posted: Tue Oct 08, 2002 12:35 pm
by nielsene
Hmm, no that's not correct, Coco
re: while (!feof($bfp) && stuff)
when bfp reaches the end of the file feof will return true. not true equals false. False and anything equals false, php, like c will short-circuit this and stop testing the condition when it detects a "false and" construct. While(false) should then terminate the loop.
Posted: Tue Oct 08, 2002 12:46 pm
by nielsene
I know this doesn't really answer the question, but maybe solve the problem a different way:
Code: Select all
<?php
function isbanned()
{
$banfile = "banfile.txt";
$localip = $_SERVERї'REMOTE_ADDR'];
if (!file_exists("$banfile")) {
$bfp = fopen("$banfile", "a");
fclose($bfp);
}
$bfp = fopen("$banfile","r") or die ("error when opening banfile.txt");
$lines = fread($bfp,filesize($banfile));
fclose($bfp);
$lines = explode("\n",$lines); // EDIT: left out by accident
return in_array($localip,$lines);
}
?>
If you want to debug the current version, have you tried echo'ing what buffer is on every pass? Have you checked for an error on the "pump-priming" first fgets? In some languanges an error will unset the EOF marking so reading an empty file may cause the problem you're experiencing, I haven't tried it in PHP. Actually now that I type that, I suspect that's your problem. Test for !feof($bfp) before doing the fgets outside of the while......
Posted: Tue Oct 08, 2002 2:31 pm
by Coco
ok maybe i wrong
hsould take more meds i reckon... at least the nhs agrees with the fact that im a nutball

Posted: Tue Oct 08, 2002 8:53 pm
by hob_goblin
He said he already solved it, but this is what i'd do:
Code: Select all
<?php
function isbanned()
{
$banfile = "banfile.txt";
if (!file_exists("$banfile")) {
$bfp = fopen("$banfile", "a");
fclose($bfp);
}
$lines = file("$banfile");
return in_array($_SERVERї'REMOTE_ADDR'],$lines);
}
?>
Posted: Wed Oct 09, 2002 6:03 am
by GoodspeeÐ
Thx 4 all the suggestions guys. I did it with the in_array and file functions.
Code: Select all
$localip = $_SERVERї'REMOTE_ADDR'];
if (!file_exists("banfile.txt")) {
$bfp = fopen("banfile.txt", "a");
fputs($bfp,"0");
fclose($bfp);
}
$banfile = file("banfile.txt");
if (in_array($localip,$banfile)) {
return 1;
}
else {
return 0;
}
Posted: Thu Oct 10, 2002 8:11 am
by superwormy
On another note, you should be careful about using IP addresses to ban users. IP addresses are not reliable for a number of reasons.
If you're on Dial-Up, every reconnection gives you a diff IP.
People behind Proxy servers all appear to have the same IP, so you could be banning an entire company network by banning one IP address.
AOL uses wierd proxying connections, meaning that AOL users can possibly appear to have a different IP address EVERY TIME THEY VISIT A NEW PAGE. ie, index.html might report me as 255.64.64.44, while the next page I visit I might appear as 255.35.22.15 or whatever.
Posted: Thu Oct 10, 2002 9:44 am
by Takuma
If you're on Dial-Up, every reconnection gives you a diff IP.
Not true, you can have stable IP address even if you are using a Dial Up connection

Posted: Thu Oct 10, 2002 9:56 am
by twigletmac
How your IP address changes/remains stable/does anything is down to your ISP (whether at home or at work). The fact remains that unless you are very sure that each of your users has a unique IP address that doesn't change and which they don't share with anyone else, banning IP's as a method of preventing people from gaining access to particular areas of a site is very unreliable.
Mac