Help - Search - Members - Calendar
Full Version: [resolved] BAB Anti-Spam Javascript
Codegrrl.com Forums > Script Help > Tutorial Help
retrowings
I've been having quite a bit of trouble with spam on my BAB Blog, so I tried following the advice here:

http://codegrrl.com/forums/index.php?showtopic=13407

I was just wondering how this javascript works, and whether something is supposed to show up before the submit button? Nothing is showing up for me sad.gif

Here's my process.php page, I'm not sure if the javascript code is in the right place or not...

CODE

<?php

if (!isset($_POST['spamtest']) || $_POST['spamtest'] != "retrobella") {
   echo "<p>JavaScript must be enabled to comment here due to spam restrictions in place.</p>";
   exit;
}


if (isset($_POST['submit_comment'])) {

    if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comment'])) {
        die("You have forgotten to fill in one of the required fields! Please make sure you submit a name, e-mail address and comment.");
    }

    $entry = htmlspecialchars(strip_tags($_POST['entry']));
    $timestamp = htmlspecialchars(strip_tags($_POST['timestamp']));
    $name = htmlspecialchars(strip_tags($_POST['name']));
    $email = htmlspecialchars(strip_tags($_POST['email']));
    $url = htmlspecialchars(strip_tags($_POST['url']));
    $comment = htmlspecialchars(strip_tags($_POST['comment']));
    $comment = nl2br($comment);

    if (!get_magic_quotes_gpc()) {
        $name = addslashes($name);
        $url = addslashes($url);
        $comment = addslashes($comment);
    }

    if (!eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
         die("The e-mail address you submitted does not appear to be valid. Please go back and correct it.");
    }

    mysql_connect ('localhost', 'rhiface1_blog', 'aurora');
    mysql_select_db ('rhiface1_blog');

    $result = mysql_query("INSERT INTO php_blog_comments (entry, timestamp, name, email, url, comment) VALUES ('$entry','$timestamp','$name','$email','$url','$comment')");

    header("Location: single.php?id=" . $entry);
}
else {
    die("Error: you cannot access this page directly.");
}
?>


Amelie
Nothing is supposed to show up before the submit button - it's just a little hidden part put into the comment form that only JavaScript enabled browsers (which bots generally aren't) can see. Your comment script checks for this hidden part (and yes, it's in the correct place) and if your browser or the bot hasn't "seen" it, it'll stop them from submitting a comment.

You can test it by disabling JS in your browser. This is easy in Firefox, just go to Tools > Options, click on Content at the top and then uncheck "Enable JavaScript". Try leaving a comment and if the protection is working, it will stop you until you turn your JS back on. smile.gif
retrowings
Ahh I see smile.gif I was unsure how to test it to make sure it was working, and it's working perfectly smile.gif

Thanks Amelie!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.