reCAPTCHA verification

Discussion in 'PHP' started by Connor Beaton, Aug 2, 2009.

  1. #1
    Hey all,

    I'm having a bit of a problem with a custom commenting system for my website. See, when you submit a comment, you have to fill in a reCaptcha to verify that you're not a spambot. If it returns false, you see an error: however, I want to keep the comment box filled out with whatever comment the person tried to submit.

    For example, if somebody writes a comment, fills out the CAPTCHA wrong, hits submit, it returns a CAPTCHA error but the comment box is now empty. I want to keep the comment box intact.

    The main issue here is that my code works like this:

    	        $resp = recaptcha_check_answer ($privatekey,
    	                                $_SERVER["REMOTE_ADDR"],
    	                                $_POST["recaptcha_challenge_field"],
    	                                $_POST["recaptcha_response_field"]);
    	        if (!$resp->is_valid) {
    	            header("location: http://zconnect.org.uk/article/".$action."&error=1#comment"); // If it's wrong, redirect to the post comment section of the article but display an error
    	        } else {
    	            // Insert the comment
    	        }
    Code (markup):
    Because I'm using header(), the POST data is not kept intact. What can I do to fix this?
     
    Connor Beaton, Aug 2, 2009 IP
  2. linkinpark2014

    linkinpark2014 Peon

    Messages:
    153
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    post the data or use get to store ur data..and then recall it inside ur text box/area
    like:
    <textarea name=mycomment><?php echo $_POST['mydata']; ?></textarea>

    so after you post the comment if it was wrong it should pass $_POST['mydata'] value inside the textarea again..
     
    linkinpark2014, Aug 2, 2009 IP
  3. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Two options:

    A) don't do the redirect and simply show the form again on the same page, then use the $_POST variables as in the post above. (With 1 distinction, be sure to htmlspecialchar() the $_POST fields).

    B) Put your $_POST data in a session, then use those values on the redirected page and be sure to unset the session data again.

    If I were to choose, it would be option A.
     
    premiumscripts, Aug 3, 2009 IP
  4. gasper000

    gasper000 Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    <input name="comment" type="text" value="<?php echo (isset($_POST['comment'])) ? $_POST['comment'] : ''; ?>" />
    Code (markup):
     
    gasper000, Aug 3, 2009 IP