Page 1 of 1

Code worked, now broken... what's changed in PHP upgrade?

Posted: Thu Apr 01, 2010 9:53 am
by Peter_LT
Hello Folks,

For my bad robots trap, I have this controlling code. When the number of IP addresses reaches
above 250, it's meant to remove the first 50 lines, leaving the latest 200 bad fellahs behind.
The file then hovers at around 3Kb in size. Now, I have a blacklist.dat that's 0 bytes, but I
don't know where to begin to solve this...

The server is running PHP 5.2.13

Code: Select all

<?php
error_reporting(E_ALL ^ E_NOTICE);
extract($_SERVER);
$badbot = 0;
$filename = ($_SERVER["DOCUMENT_ROOT"] . "/blacklist.dat");
$fp = fopen($filename, "r") or die ("Error opening file ...");
while ($line = fgets($fp,255)) {
	$u = explode(" ",$line);
	if (ereg($u[0],$REMOTE_ADDR)) {$badbot++;}
	}
	fclose($fp);
	if ($badbot == 0) {
		$fp = fopen($filename,'a+');
		fwrite($fp,"$REMOTE_ADDR \n");
		fclose($fp);
		$filearray=file($filename);
		$fp = fopen($filename,"w");
		$c=count($filearray);
		if ($c >= 250) {
			for($i=($c < 200 ? 0 : $c-200);$i < $c;++$i) {
				fputs($fp,$filearray[$i]);
			}
			fwrite($fp,"\t\t\t\t\t\t\t\t\t\ $datum \n");
		}
		fclose($fp);
	}
echo '<html>
<head>
<title>X Mail For Badly-Behaved Bots</title>
</head>
<body>
	<p>If there is nothing to see, what are you doing here ?</p>
	<p><a href="http://caronia2.info/">Go home.</a></p>
</body>
</html>';
?>
Thanks in advance for any help you can give.

Re: Code worked, now broken... what's changed in PHP upgrade

Posted: Thu Apr 01, 2010 12:24 pm
by cpetercarter
Have you checked the permissions on blacklist.dat to make sure that it is writeable by php?

Re: Code worked, now broken... what's changed in PHP upgrade

Posted: Thu Apr 01, 2010 12:37 pm
by mikosiko
I have a blacklist.dat that's 0 byte
therefore... (I may be wrong... but...)

Code: Select all

               
               $fp = fopen($filename,"w");   // HERE YOU WIPE-OUT YOUR FILE
                $c=count($filearray);   // AND BECAUSE YOUR FILE HAS NO ELEMENTS ---> $c=0
                if ($c >= 250) {
                        for($i=($c < 200 ? 0 : $c-200);$i < $c;++$i) {
                                fputs($fp,$filearray[$i]);
                        }
                        fwrite($fp,"\t\t\t\t\t\t\t\t\t\ $datum \n");
                }
                fclose($fp);  //AND HERE YOU CLOSE A 0 bytes file
...

so maybe re-writing in this way could work

Code: Select all

               
                $c=count($filearray);   
                if ($c >= 250) {
                       $fp = fopen($filename,"w");  
                        for($i=($c < 200 ? 0 : $c-200);$i < $c;++$i) {
                                fputs($fp,$filearray[$i]);
                        }
                        fwrite($fp,"\t\t\t\t\t\t\t\t\t\ $datum \n");
                       fclose($fp);  
               }
  
...

Re: Code worked, now broken... what's changed in PHP upgrade

Posted: Thu Apr 01, 2010 12:39 pm
by Peter_LT
cpetercarter wrote:Have you checked the permissions on blacklist.dat to make sure that it is writeable by php?
The file has a "776" value - world: read/write - does your question have something to do
with "safe mode" being on or off? I've noticed recently that it has been changed to "on"...

Re: Code worked, now broken... what's changed in PHP upgrade

Posted: Thu Apr 01, 2010 2:40 pm
by Peter_LT
mikosiko wrote:so maybe re-writing in this way could work

Code: Select all

               
                $c=count($filearray);   
                if ($c >= 250) {
                       $fp = fopen($filename,"w");  
                        for($i=($c < 200 ? 0 : $c-200);$i < $c;++$i) {
                                fputs($fp,$filearray[$i]);
                        }
                        fwrite($fp,"\t\t\t\t\t\t\t\t\t\ $datum \n");
                       fclose($fp);  
               }
  
...
Many thanks, that seems a more logical way - I've re-written the routine, and now
waiting for the next bad-bots to trigger it and see what happens...