Hey PHPBuilders!
I'm John, and I'm new to your forums...
I have a BAD spam problem on my site.. I have a comments system which me and my friend made in PHP and SQL... it works great!
There's one huge problem though, people keep "spamming" it.. posting loads of messages like "name: sosioiosojsiojsijosi message: djdjdkkdjjdkjkll" over and over and over, it takes me forever to delete them all. I can ban their IP addresses manually, but there is no actual control to stop them from submitting LOADS and LOADS over and over.
I asked my friend about it, and he gave me this code to work from:
<?php
//VERY IMPORTANT: The folder "antispam" must exist on your server for this to work.
//Right, I would do it like so:
//(php source with comments)
$their_IP=$_SERVER["REMOTE_ADDR"];
//get their IP
if(file_exists("antispam/".$their_IP)){
//check for a file called their IP in an 'antispam' folder (or whatever you want to call it)
if(!$ip_file=file("antispam/".$their_IP)){
//if so, get the contents of that file as a string
echo '<BR>Server error.<BR>';
exit;
}
//if there's an error opening the file, display an error message and exit (note the ! operator in the if statement above)
trim($ip_file[0]);
//strip any carriage returns and white space; the above should now contain just a date
if((date("dmYHis")-$ip_file[0])<10){
//get the current date and subtract the old date from the current date
echo '<BR>You cannot post a comment more than once every ten minutes.<BR>';
exit;
}
//if less than 10 minutes, display an explanation and exit
}
//otherwise it's all good, so just end the if statement
//similarly, if a file with their IP address didn't exist, it can be assumed they didn't post within ten minutes
//because of the next bit we're going to do
if(!$S_file=fopen("antispam/".$their_IP,"w")){
//open a file named the same as their ip; if doesn't exist, it will be created
echo '<BR>Server error.<BR>';
exit;
//if there's an error, print a message about it and exit
}
fputs($S_file, date("dmYHis"));
//put in the date; if the file existed already, it will be overwritten completely
fclose($S_file);
//close the file
//comment posting code would then go here
//note that this doesn't involve error handling for if their IP file contains something other than a date
//which it shouldn't
//mc
?>
You can probably get the idea of it.. it's meant to stop the user from posting, and limit them to one post every ten minutes. The only problem is, it doesn't work, it's very glitchey (yes I did right the folder 777), and I can't put my finger on the problem (I'm still pretty new to PHP).
So If someone could correct my friend's coding, I would be extreamly greatful!
All help is appreciated!
Thanks,
John