Hi there guys,
I decided to put away the crack pipe, then I fixed a lot of the problems with the code, for instance, it seems that the db method is working opposite to the flatfile. The flatfile did certain things if it didn't match an existing record, and I had written the mysql version to do things if it did match, so I reversed a lot of what needed to be done.
Here's what I have now. Comments concerning function and efficiency is appreciated:
include("../config.php"); //Depending on your directory structure, you may need to change this//
/*Setting all of the server variables*/
$REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
$REQUEST_URI = $_SERVER['REQUEST_URI'];
$REQUEST_METHOD = $_SERVER['REQUEST_METHOD'];
$HTTP_REFERER = $_SERVER['HTTP_REFERER'];
$HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
$SERVER_PROTOCOL = $_SERVER['SERVER_PROTOCOL'];
echo("<html>
<head><title>Tag, you're it!</title></head>
<body>
<p>There is nothing here to see. So what are you doing here ?</p>
<p><a href='http://www.domain.com'>Go home.</a></p>");
$habitual = 0;
$query = "SELECT remote_addr, hits FROM ".$prefix."botpot";
$result = mysql_query ($query) or die('MySQL error: ' . mysql_error() . '<hr/>' . $query);
$result_rows = mysql_num_rows($result);
/*If no record, then it must be new!*/
if($result_rows!=0){
$result = mysql_query ($query);
while ($row = mysql_fetch_assoc ($result)) {
if($REMOTE_ADDR == $row['remote_addr']{
$habitual++;
$hits = $row['hits']+1;
}
}
}
if ($habitual == 0) { /* we just see a new bad bot not yet listed ! */
/* send a mail to hostmaster */
$tmestamp = time();
$datum = date("Y-m-d (D) H:i:s",$tmestamp);
$from = "badbot-watch@domain.com";
$to = "you@domain.com";
$subject = "Alert: bad robot";
$msg = "A bad robot hit $REQUEST_URI $datum \n\n";
$msg .= "address is $REMOTE_ADDR, agent is $HTTP_USER_AGENT\n";
mail($to, $subject, $msg, "From: $from");
/* append bad bot address data to blacklist database: */
$sql = "INSERT INTO ".$prefix."botpot (id, hits, remote_addr, date, request_method, request_uri, server_protocol, http_referer, http_user_agent) VALUES ('NULL', '1', '$REMOTE_ADDR', '$tmestamp', '$REQUEST_METHOD', '$REQUEST_URI', '$SERVER_PROTOCOL', '$HTTP_REFERER', '$HTTP_USER_AGENT')";
$result = mysql_query($sql) or die('MySQL error: ' . mysql_error() . '<hr/>' . $sql);
}else{
mysql_query("UPDATE ".$prefix."botpot SET hits = $hits WHERE remote_addr = '$REMOTE_ADDR'") or die("Cannot query the database.<br><br>" . mysql_error());
}
echo("</body>
</html>");
This code is supposed to do the following:
1) Check to see if we've seen this IP before
2) If we haven't, then we enter the information into the db and email the contact.
3) If we have, we simply increase the hits field by 1.
thanks,
json