Forums | MacLife

You are not logged in.

#1 2007-12-26 9:23 pm

tokyoite
Member
From: Tokyo
Registered: 2003-08-01
Posts: 64
Website

PHP CAPTCHA

I'm trying to set up a CAPTCHA on my site. The e-mail form was
previously hijacked by a "bot" that was filling my inbox with spam.

I've got it set up at http://www.timyoungonline.com/email.php . The
problem is, even if I enter the CAPTCHA words correctly, it tells me
that I've entered them incorrectly. Somehow it isn't detecting correct
answers.

The coding is in PHP. I'm no programmer; I got the e-mail form PHP
from someone else a few years ago. It was meant to hide the "mailto"
address from spammers. In the spam arms race, unfortunately, that's
not a foolproof approach anymore...

At any rate, this is what I've got so far (editing out my personal
code numbers):

<?php
          require_once('recaptchalib.php');
          $publickey = "..."; // you got this from the signup page
          echo recaptcha_get_html($publickey);
         ?>

      </p>
      <FORM METHOD=POST>
        <INPUT TYPE=HIDDEN NAME="ACTION" VALUE="send-mail">
        Your E-mail:
        <INPUT TYPE=TEXT NAME="EMAIL">
        <br>
        Message Subject:
        <INPUT TYPE=TEXT NAME="SUBJECT">
        <p> Message:<br>
          <TEXTAREA NAME="MESSAGE" ROWS=5 COLS=50></TEXTAREA>
        <p>
          <INPUT name="SUBMIT" TYPE=SUBMIT>
          *
          <INPUT name="RESET" TYPE=RESET>
      </FORM>


<?php
  // Your E-mail Address
  $MYEMAIL = "fictionaladdress@domain.com";

  if ($ACTION == "send-mail")
  {
    require_once('recaptchalib.php');
    $privatekey = "...";
    $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

    if (!$resp->is_valid)
    {
      die ("The reCAPTCHA wasn't entered correctly. Go back and try it
again." .
      "(reCAPTCHA said: " . $resp->error . ")");
    }
    else
    {
      mail($MYEMAIL,$SUBJECT,$MESSAGE,"From: $EMAIL");
      echo "<h2>Thanks for your message!</h2>\n";
    }
  }
?>

Anyone familiar with PHP recognize any problems here?

Last edited by tokyoite (2007-12-26 9:23 pm)

Offline

 

#2 2007-12-26 10:09 pm

Miles
Now I fight for wisdom!
Administrator
From: Michigan
Registered: 2001-07-21
Posts: 4482
Website

Re: PHP CAPTCHA

tokyoite wrote:

it tells me that I've entered them incorrectly.

Does it actually say, "The reCAPTCHA wasn't entered correctly"?  If that's the case, the error message that it gives would have been helpful.

The second require_once('recaptchalib.php') is redundant, you're already requiring it at the top.
The recaptcha_get_html() part should probably be inside the <form> tags.
Stop using register_globals.

Offline

 

#3 2007-12-27 6:16 am

tokyoite
Member
From: Tokyo
Registered: 2003-08-01
Posts: 64
Website

Re: PHP CAPTCHA

Just moving the recaptcha_get_html part to within the <form> tags fixed the problem. Thanks!

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson