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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Peter_LT
Forum Newbie
Posts: 5
Joined: Mon May 18, 2009 8:21 am

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

Post 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.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

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

Post by cpetercarter »

Have you checked the permissions on blacklist.dat to make sure that it is writeable by php?
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

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

Post 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);  
               }
  
...
Peter_LT
Forum Newbie
Posts: 5
Joined: Mon May 18, 2009 8:21 am

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

Post 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"...
Peter_LT
Forum Newbie
Posts: 5
Joined: Mon May 18, 2009 8:21 am

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

Post 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...
Post Reply