12 Replies Latest reply on Feb 22, 2010 5:51 AM by nmbnmb

    PHP Recaptcha.net onto Dreamweaver Built Form

    GD WebDev Level 1

      Hi All,

       

      I've built a basic form for an online petition in dreamweaver using the form fields Name, Village, Comments.

      The form is working correctly.

       

      Over the past few days i've had an issue with spam and therefore would like to install the recaptcha.net application into the script dreamweaver built script.

      Instructions given on how to do this are here: http://wiki.recaptcha.net/index.php/Overview#PHP

       

      I've followed the everything but it doens't seem to verify the recaptcha and bypasses it still submitting the form.

       

      First of all i have downloaded all the files that i required for the script to work and followed the instructions.

      i've inserted the first set of script required into my code shown in red below:

       

      <form action="<?php echo $editFormAction; ?>" method="POST" name="Petition" id="Petition">

       

      <form method="post" action="verify.php">
         <?php
           require_once('recaptchalib.php');
           $publickey = "my public key is here"; // you got this from the signup page
           echo recaptcha_get_html($publickey);
         ?>
            <table width="100%" border="0">
              <tr>
                <td width="108" class="style3"><strong>Full Name:</strong></td>
                <td width="660"><input name="Name" type="text" class="form" id="Name" size="50">
                    <input name="ID" type="hidden" id="ID">          </td>
              </tr>
              <tr>
                <td valign="top"><span class="style3"><strong>Village:</strong></span><br></td>
                <td><input name="Village" type="text" class="form" id="Village" size="50"></td>
              </tr>
              <tr>
                <td valign="top" class="style3"><strong>Comment(s):</strong></td>
                <td><textarea name="Comments" cols="55" rows="5" class="form" id="Comments"></textarea>            </td>
              </tr>
              <tr>
                <td valign="top"> </td>
                <td>< </td>
              </tr>
              <tr>
                <td valign="top"> </td>
                <td><input name="Submit" type="submit" onClick="MM_validateForm('Name','','R','Village','','R','Comments','','R');return document.MM_returnValue" value="Submit">
                  <input type="hidden" name="MM_insert" value="Petition"></td>
              </tr>
            </table>
        

          </form>

       

       

      The second piece of the instructions explains that i need to create a verify.php and i've done that with the code that it as given me to shown in the instructions.

       

      Any ideas as to why i'm not working?

        • 1. Re: PHP Recaptcha.net onto Dreamweaver Built Form
          polar_bare Level 4

          This might be best suited for the recaptcha help site on google groups. How about a link to the page?

           

          Jim

          • 2. Re: PHP Recaptcha.net onto Dreamweaver Built Form
            nmbnmb Level 2

            <form action="<?php echo $editFormAction; ?>" method="POST" name="Petition" id="Petition">

             

            <form method="post" action="verify.php">
               <?php
                 require_once('recaptchalib.php');
                 $publickey = "my public key is here"; // you got this from the signup page
                 echo recaptcha_get_html($publickey);
               ?>

             

            You have a <form> tag within a <form> tag. Remove the second form tag <form method="post" action="verify.php">. Then in the script for <?php echo $editFormAction; ?> add the reCaptcha server side code (2nd part of the code from reCaptcha).

            1 person found this helpful
            • 3. Re: PHP Recaptcha.net onto Dreamweaver Built Form
              GD WebDev Level 1

              I still have an issue with this. I've followed what you have said but still seem to have a problem.

               

              I've placed the first bit of code where as you suggested:

               

               

              <form action="<?php echo $editFormAction; ?>" method="POST" name="Petition" id="Petition">

               

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

                    <table width="100%" border="0">
                      <tr>
                        <td width="108" class="style3"><strong>Full Name:</strong></td>
                        <td width="660"><input name="Name" type="text" class="form" id="Name" size="50">
                            <input name="ID" type="hidden" id="ID">          </td>
                      </tr>
                      <tr>
                        <td valign="top"><span class="style3"><strong>Village:</strong></span><br></td>
                        <td><input name="Village" type="text" class="form" id="Village" size="50"></td>
                      </tr>
                      <tr>
                        <td valign="top" class="style3"><strong>Comment(s):</strong></td>
                        <td><textarea name="Comments" cols="55" rows="5" class="form" id="Comments"></textarea>            </td>
                      </tr>
                      <tr>
                        <td valign="top"> </td>
                           
                 </td>
                      </tr>
                      <tr>
                        <td valign="top"> </td>
                        <td><input name="Submit" type="submit" onClick="MM_validateForm('Name','','R','Village','','R','Comments','','R');return document.MM_returnValue" value="Submit">
                          <input type="hidden" name="MM_insert" value="Petition"></td>
                      </tr>
                    </table>
               

                  </form>

               

               

              Then the second server side code where you suggested:

               

              <form action="<?php echo $editFormAction;

               

              require_once('recaptchalib.php');
              $privatekey = "my key is here";
              $resp = recaptcha_check_answer ($privatekey,
                                             $_SERVER["REMOTE_ADDR"],
                                             $_POST["recaptcha_challenge_field"],
                                             $_POST["recaptcha_response_field"]);
              if (!$resp->is_valid) {
                 // What happens when the CAPTCHA was entered incorrectly
                 die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
                      "(reCAPTCHA said: " . $resp->error . ")");
              } else {
                 // Your code here to handle a successful verification
              }

              ?>
              " method="POST" name="Petition" id="Petition">

               

              <?php
                   require_once('recaptchalib.php');
                   $publickey = "6LeLmQkAAAAAANZVrybpVWBWECtAtqaM0J28xWnK"; // you got this from the signup page
                   echo recaptcha_get_html($publickey);
                 ?>
                    <table width="100%" border="0">
                      <tr>
                        <td width="108" class="style3"><strong>Full Name:</strong></td>
                        <td width="660"><input name="Name" type="text" class="form" id="Name" size="50">
                            <input name="ID" type="hidden" id="ID">          </td>
                      </tr>
                      <tr>
                        <td valign="top"><span class="style3"><strong>Village:</strong></span><br></td>
                        <td><input name="Village" type="text" class="form" id="Village" size="50"></td>
                      </tr>
                      <tr>
                        <td valign="top" class="style3"><strong>Comment(s):</strong></td>
                        <td><textarea name="Comments" cols="55" rows="5" class="form" id="Comments"></textarea>            </td>
                      </tr>
                      <tr>
                        <td valign="top"> </td>
                        <td>
                <noscript>
                <br>
                 
                </noscript>
              </td>
                      </tr>
                      <tr>
                        <td valign="top"> </td>
                        <td><input name="Submit" type="submit" onClick="MM_validateForm('Name','','R','Village','','R','Comments','','R');return document.MM_returnValue" value="Submit">
                          <input type="hidden" name="MM_insert" value="Petition"></td>
                      </tr>
                    </table>
                

                  </form>

               

              The error i'm now receiving is on the page is: <form action="/petition/index.php?The reCAPTCHA wasn't entered correctly. Go back and try it again.(reCAPTCHA said: incorrect-captcha-sol)

              • 4. Re: PHP Recaptcha.net onto Dreamweaver Built Form
                nmbnmb Level 2

                There should be 2 files. One file is the form and the other file is the script. What is the file name of the script that will be processing the form? The script is where to put the second server side code.

                • 5. Re: PHP Recaptcha.net onto Dreamweaver Built Form
                  GD WebDev Level 1

                  Yes, a verify.php file which i tried in my first attempt of doing this which i mentioned in my first post.

                   

                  But the problem was i was putting a form inside a form:

                   

                  <form action="<?php echo $editFormAction; ?>" method="POST" name="Petition" id="Petition">

                   

                  <form method="post" action="verify.php">
                     <?php
                       require_once('recaptchalib.php');
                       $publickey = "my public key is here"; // you got this from the signup page
                       echo recaptcha_get_html($publickey);
                     ?>

                   

                  Therefore how would i intergrate/combine the two together for it to function correctly, and verify the verifiy.php file at the same time?


                  • 6. Re: PHP Recaptcha.net onto Dreamweaver Built Form
                    nmbnmb Level 2
                    <form action="<?php echo $editFormAction; ?>" method="POST" name="Petition" id="Petition">

                     

                    In the above code, what is the value of $editFormAction? Before adding reCAPTCHA, was the form working?

                    • 7. Re: PHP Recaptcha.net onto Dreamweaver Built Form
                      GD WebDev Level 1

                      Yes, the form is a working form.

                       

                      It was created in Dreamweaver using its Server Behaviours in the Applications window.

                      Its basically a form that once completed and submitted, it is then added into a database. As its a petition i've created I've then used the Repeat Region option in DW to show a list of submissions on the webpage. The same principle as submitting and displaying an online blog.

                       

                      The part of the post action that you are referring to as been automatically generated by DW from the Insert Record from the Recordset.

                       

                      So in a nutshell i've used DW to generate everything, i've not used any external bespoke form submitters from any third party. Its all been created using DW's built in applications.

                      • 8. Re: PHP Recaptcha.net onto Dreamweaver Built Form
                        nmbnmb Level 2

                        In the same file, insert the 2nd server code before the $insertSQL statement like this:

                         

                        require_once('recaptchalib.php');
                        $privatekey = "your private key ";
                        $resp = recaptcha_check_answer ($privatekey,
                                                       $_SERVER["REMOTE_ADDR"],
                                                       $_POST["recaptcha_challenge_field"],
                                                       $_POST["recaptcha_response_field"]);

                         

                        if (!$resp->is_valid) {
                           // What happens when the CAPTCHA was entered incorrectly
                        } else {
                           // Your code here to handle a successful verification


                          $insertSQL = sprintf("INSERT INTO dreamweaver (id, email) VALUES (%s, %s)",

                         

                        #####

                         

                        With the last } move it after the header statement like this:

                         

                          header(sprintf("Location: %s", $insertGoTo));
                          }
                        }

                        1 person found this helpful
                        • 9. Re: PHP Recaptcha.net onto Dreamweaver Built Form
                          GD WebDev Level 1

                          Ahhhh, seem to be getting somewhere with this now.

                           

                          I've tested it and it creates the record, validates and works, However a small problem with after submission it comes up at the top of the page with this error:

                           

                          Warning: Cannot modify header information - headers already sent by (output started at /Connections/Petition.php:2) in /petition/index.php on line 64

                           

                          Lines 64 is this piece of code: header(sprintf("Location: %s", $insertGoTo));

                           

                          Here is how the 2nd piece of server code looks:

                           

                           

                          <?php require_once('Connections/Petition.php'); ?>
                          <?php
                          function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
                          {
                            $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

                            switch ($theType) {
                              case "text":
                                $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                                break;   
                              case "long":
                              case "int":
                                $theValue = ($theValue != "") ? intval($theValue) : "NULL";
                                break;
                              case "double":
                                $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
                                break;
                              case "date":
                                $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                                break;
                              case "defined":
                                $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
                                break;
                            }
                            return $theValue;
                          }

                          $editFormAction = $_SERVER['PHP_SELF'];
                          if (isset($_SERVER['QUERY_STRING'])) {
                            $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
                          }

                          if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "Petition")) {
                           
                            require_once('recaptchalib.php');
                          $privatekey = "6LeLmQkAAAAAAE8tP_lR-mm3MdFFaK_XRgrkLjZc";
                          $resp = recaptcha_check_answer ($privatekey,
                                                         $_SERVER["REMOTE_ADDR"],
                                                         $_POST["recaptcha_challenge_field"],
                                                         $_POST["recaptcha_response_field"]);
                          if (!$resp->is_valid) {
                             // What happens when the CAPTCHA was entered incorrectly
                             die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
                                  "(reCAPTCHA said: " . $resp->error . ")");
                          } else {
                             // Your code here to handle a successful verification

                           
                            $insertSQL = sprintf("INSERT INTO petition (ID, Name, Area, Comment) VALUES (%s, %s, %s, %s)",
                                                 GetSQLValueString($_POST['ID'], "int"),
                                                 GetSQLValueString($_POST['Name'], "text"),
                                                 GetSQLValueString($_POST['Village'], "text"),
                                                 GetSQLValueString($_POST['Comments'], "text"));

                            mysql_select_db($database_Petition, $Petition);
                            $Result1 = mysql_query($insertSQL, $Petition) or die(mysql_error());

                            $insertGoTo = "sent.php";
                            if (isset($_SERVER['QUERY_STRING'])) {
                              $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
                              $insertGoTo .= $_SERVER['QUERY_STRING'];
                            }
                          Line 64 is here>  header(sprintf("Location: %s", $insertGoTo));
                          }
                          }
                          mysql_select_db($database_Petition, $Petition);
                          $query_Create = "SELECT * FROM petition ORDER BY ID DESC";
                          $Create = mysql_query($query_Create, $Petition) or die(mysql_error());
                          $row_Create = mysql_fetch_assoc($Create);
                          $totalRows_Create = mysql_num_rows($Create);
                          ?>

                          • 10. Re: PHP Recaptcha.net onto Dreamweaver Built Form
                            nmbnmb Level 2

                            Remove line 64 if it wasn't there initially.

                            • 11. Re: PHP Recaptcha.net onto Dreamweaver Built Form
                              GD WebDev Level 1

                              Thats great thanks for your help with this much appreciated.

                               

                              Is there away i can link to my own error page by changing this script:

                               

                              if (!$resp->is_valid) {
                                 // What happens when the CAPTCHA was entered incorrectly
                                 die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
                                      "(reCAPTCHA said: " . $resp->error . ")");
                              } else {

                              • 12. Re: PHP Recaptcha.net onto Dreamweaver Built Form
                                nmbnmb Level 2

                                Sure. Change the url in the header statement:

                                 

                                if (!$resp->is_valid) {
                                   // What happens when the CAPTCHA was entered incorrectly
                                   header("Location: http://www.yourdomain.com/error.php");

                                   exit;
                                } else {