Valadation in guestbook...

Discussion in 'PHP / Perl / Ruby on Rails' started by Rawns, Sep 8, 2006.

  1. Rawns Guest

    Hay ho guys.

    I'm trying to add a valadation field for my guestbook so it stops getting attacked by bots. On the guestbook form, i've addded an extra text box called 'antispam' where posters type in NOTSPAM to allow the post. How and where would I add some code that checks this text box contains NOTSPAM and allow the entery to be posted? If it does not contain NOTSPAM, an error message is displayed?

    Anyhoo, this is my guestbook code:

    PHP:
    <?php
        import_request_variables
    ('p');
        if (isset(
    $firstname) && isset($surname) && isset($email))
        {
            
    $dberror="";
            
    $result=add_to_database($firstname,$surname,$email,$comment,$dberror);
            if (!
    $result)
            {
                print
    "Error: $dberror<br />";
            }
            else
            {
                print
    "<center>Thanks for signing <b>$firstname $surname</b></center><br />";
            }
        }
        else
        {
            
    write_form();
        }
        function 
    add_to_database($f,$s,$e,$c,&$dbe)
        {
            
    $user="XXXXXXXX";
            
    $pass="XXXXXXXX";
            
    $db="guestbook";
            
    $link=mysql_connect("sql02",$user,$pass);
            if (!
    $link)
            {
                
    $dberror="Could not connect to MySQL server";
                return 
    false;
            }
            If (!
    mysql_select_db($db,$link))
            {
                
    $dbe="Could not connect to $db";
                return 
    false;
            }

            
    $query="INSERT INTO guestbook (firstname,surname,email,comment) VALUES ('$f','$s','$e','$c')";
            {
                if (!
    mysql_query($query$link))
                {
                    
    $dbe=mysql_error();
                    return 
    false;
                }
                return 
    true;
            }
        }
        function 
    write_form()
        {
            print
    "<background='images/background.png'>";
            print
    "<form method='post' action='$PHP_SELF'>";
            print
    "<strong> Firstname:</strong><br>";
            print
    "<input type ='text' name='firstname'/><br>";
            print
    "<strong>Surname:</strong><br>";
            print
    "<input type ='text' name='surname'/><br>";
            print
    "<strong> E-mail:</strong><br>";
            print
    "<input type ='text' name='email'/><br>";
            print
    "<strong>Comment:</strong><br>";
            print
    "<textarea name='comment' cols='50' rows='5'></textarea><br>";
            print
    "<strong>Anti-Spam Field</strong><br>";
            print
    "<strong>Enter NOTSPAM into the text area:</strong><br>";
            print
    "<input type = 'text' name='antispam'/><br>";
            print
    "<input type ='submit' value='Sign!'/><input type=\"reset\" name=\"Reset\" value=\"Reset\"/><br>";
            print
    "</form>";
        }
    ?>
  2. Nick Irvine Secretly the main man

    All you need do is add:

    PHP:
    if ($antispam==NOTSPAM) {
    // put add to database code here
    } else {
    echo (
    "You havent passed the spam test!!");
    }
  3. Rawns Guest

    Sorted. Once I added your bit Neal, I had to add:

    PHP:
    $antispam $_POST['antispam'];
    ...and change...

    PHP:
    if ($antispam==NOTSPAM)
    ...to...

    PHP:
    if ($antispam=="NOTSPAM")
    THanks for the help. :)

Share This Page