www.webdeveloper.com
Results 1 to 3 of 3

Thread: PHP Recaptcha Validation Issue

  1. #1
    Join Date
    May 2012
    Posts
    17

    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:
    <head>
        <link rel="stylesheet" type="text/css" href="css/styles.css" />
    </head>

    <?php
      
    require_once('recaptchalib.php');
      
    $privatekey "6LdedO4SAAAAAJiL3Lfa-c_GpT4W5Ij9M9ZGKen-";
      
    $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.\n<br /> Go back and try it again." .
             
    "\n<br />(reCAPTCHA said: " $resp->error ")");
      } else {
        
    // Your code here to handle a successful verification
      
    }


        
    //EMAIL VALIDATION
        
    function validateEmail($value){
            return 
    preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'$value);
        }
        
        
    //CHECK VARIABLES (EMPTY/NULL OR DEFAULT)
        
    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  /////////////////////////
                
                //SET HERE YOUR DESTINATION EMAIL
                //IT SHOULD BE FROM THE SAME DOMAIN WHERE SITE IS HOSTED
                
    $destination="info@khangp.com";
                
                
    //SET HERE YOUR EMAIL SUBJECT
                
    $subject$_POST['subjectSelect'];

                
    //MESSAGE DATA (HTML FORMATTED)
                
    $mailMessage.="<dt><strong>Name:</strong></dt><dd>".$_POST['name']."</dd>";
                
    $mailMessage.="<dt><strong>E-mail:</strong></dt><dd>".$_POST['email']."</dd>";
                
    $mailMessage.="<dt><strong>Comments:</strong></dt><dd>";  
                
    $mailMessage.=nl2br($_POST['message'])."</dd></dl>";
                
    $mailMessage utf8_decode($mailMessage);
                
                
    ////////////////////// END EDIT  /////////////////////////
                
                
                //SENDER EMAIL
                
    $mailFrom=$_POST['email'];
                
                
    //HEADER DATA
                
    $mailHeader="From:".$mailFrom."\nReply-To:".$_POST['name']."<".$mailFrom.">\n"
                
    $mailHeader=$mailHeader."X-Mailer:PHP/".phpversion()."\n"
                
    $mailHeader=$mailHeader."Mime-Version: 1.0\n"
                
    $mailHeader=$mailHeader."Content-Type: text/html";
                
                if ( 
    mail($destination,$subject,$mailMessage,$mailHeader) ) {
                    echo 
    '<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
    Posts
    17
    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
    Location
    Ankh-Morpork
    Posts
    19,634
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

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



Recent Articles