Results 1 to 3 of 3

Thread: PHP Recaptcha Validation Issue

  1. #1
    Join Date
    May 2012

    Question PHP Recaptcha Validation Issue

    Hello folks. I need some PHP help..obviously. I attempted to use my own logic to resolve the issue but that of course didn't work. Basically, I needed to add captcha validation which I did using "recaptcha" plugin. While it works, it's a very clumsy implementation of it, if I may say that. The code is below.

    So right now, when you fill out the form correctly on the first go, including the captcha text, it submits no problem. But say you miss a field and when you get to the captcha validation, you input the right text and hit submit. As expected, once it validates the text, it brings up the "Missing Field!" error. You go back and make appropriate changes but when you hit submit this time, the captcha validation fails and you have to refresh the image and re-submit. It makes sense why this occurs, but it's extremely annoying.

    I was hoping someone could be kind enough to modify the code or perhaps guide on how I can make it so that the captcha validation happens at the very end. Meaning, if there is a blank form and someone hits submit, it should say "Missing Fields!" or "Sorry. Not a valid Email!", basically going through the entire form validation process before it gets to the captcha validation.

    I've been playing around with the "if" statement all day and I can't seem to find a resource online. Any help would be appreciated.

    Contact Page: http://khangp.com/#contact

    PHP Code:
        <link rel="stylesheet" type="text/css" href="css/styles.css" />

    $privatekey "6LdedO4SAAAAAJiL3Lfa-c_GpT4W5Ij9M9ZGKen-";
    $resp recaptcha_check_answer ($privatekey,

          if (!
    $resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly.\n<br /> Go back and try it again." .
    "\n<br />(reCAPTCHA said: " $resp->error ")");
      } else {
    // Your code here to handle a successful verification

    function validateEmail($value){
    if ( isset($_POST['name']) && $_POST['name']!="Name" && isset($_POST['email']) && $_POST['email']!="Email" && isset($_POST['subjectSelect']) && $_POST['subjectSelect']!="" && isset($_POST['message']) && $_POST['message']!="Your comments..." ) {
    //CHECK EMAIL    
    if ( validateEmail($_POST['email']) ) {
    ////////////////////// EDIT HERE  /////////////////////////

    $mailMessage utf8_decode($mailMessage);
    ////////////////////// END EDIT  /////////////////////////
                //SENDER EMAIL
    $mailHeader=$mailHeader."Mime-Version: 1.0\n"
    $mailHeader=$mailHeader."Content-Type: text/html";
                if ( 
    mail($destination,$subject,$mailMessage,$mailHeader) ) {
    '<span class="output">Message successfully sent!<br />We will contact you shortly.<br /> Thank you!</span>';
                else echo 
    '<span class="outputred">Server error. Please, try again later</span>';
            else echo 
    '<span class="outputred">Sorry. Not a valid Email!</span>';    //EMAIL VALIDATION ERROR
        else echo 
    '<span class="outputred">Missing fields!</span>';        //VARS ERROR    

  2. #2
    Join Date
    May 2012
    I'm surprised I have not gotten even one reply. At any rate, please close this thread as I cannot seem to find a way to do it myself. I'm ditching the whole captcha method and employing a new method to prevent spam.

  3. #3
    Join Date
    Aug 2004
    There are several ways to do sequential forms, but possibly the most robust way would be:

    1. User fills out form page 1 and submits it
    2. Form-handler script for that page validates it. If everything is okay, it saves the submitted data in $_SESSION (you need to do session_start() on each page), then displays the captcha form page.
    3. User fills out captcha and submits
    4. Form-handler for captcha submission validates it, and if okay, processes the original form data from the $_SESSION array.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center