add captcha to form

Discussion in 'PHP' started by Jackie_W, Jul 15, 2008.

  1. #1
    Jackie_W, Jul 15, 2008 IP
  2. zendobi

    zendobi Active Member

    Messages:
    132
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #2
    My suggestion would be to use ReCaptcha, it is one of the best and is a free solution, they provide examples that explain everything. They require API keys, you can signup for it here: http://recaptcha.net/whyrecaptcha.html

    a quick example for your install would look like this:
    In your template for adding a URL you will have a section that looks like this:
    
          <td class="bodymd">OImage URL (Optional) (Images size 125x100 pixels)<br />
            <input type="text" name="image" size="60" value="" maxlength="100"/></td></tr>
    <tr><td class="bodymd"> Email Address:<br /><input type="text" name="email" size="50"/></td></tr>
    <tr><td class="bodymd"> Password:<br /><input type="password" name="password" size="10"/></td></tr>
    
    PHP:
    change it to:
    
          <td class="bodymd">OImage URL (Optional) (Images size 125x100 pixels)<br />
            <input type="text" name="image" size="60" value="" maxlength="100"/></td></tr>
    <tr><td class="bodymd"> Email Address:<br /><input type="text" name="email" size="50"/></td></tr>
    <tr><td class="bodymd"> Password:<br /><input type="password" name="password" size="10"/></td></tr>
    <tr><td class="bodymd">[captcha]</td></tr>
    
    PHP:
    in the top of directory.php on the top line before anything else add:
    
    <?php
    require_once('recaptchalib.php');
    $publickey = "..."; // you got this from the signup page
    $privatekey = "...";
    if($ax=="add"){
    $captcha=recaptcha_get_html($publickey);
    }
    else if($ax=="insert"){
    $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 . ")");
    }
    }
    ?>
    
    PHP:
    In order to determine how to make it show on the form in place of [captcha] I need to know if the html for the form is in directory.php or is a seperate html template. Once you tell me that i will give you the code to add to directory.php to show it on the form.
     
    zendobi, Jul 15, 2008 IP
  3. Jackie_W

    Jackie_W Well-Known Member

    Messages:
    1,964
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    115
    #3
    Thanks Mike, worked first time, saves me filtering spam every day :D
     
    Jackie_W, Jul 16, 2008 IP
  4. zendobi

    zendobi Active Member

    Messages:
    132
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #4
    Your welcome:)
     
    zendobi, Jul 16, 2008 IP
  5. buckmajor

    buckmajor Active Member

    Messages:
    574
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Cool! I needed to know how to add a reCaptcha to my form. Will this still work if I'm using Javascript to validate my form? I'll give it a go, and post back on my experience.
     
    buckmajor, Sep 6, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    Should work aslong as your form is PHP based, doesn't matter what validation you use - but I suggest if you do use JS you should also do server side validation (as JS can be disabled).
     
    danx10, Sep 6, 2010 IP
  7. buckmajor

    buckmajor Active Member

    Messages:
    574
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Damn, I've been at it for a while now, and finding it hard to install. Ok, I have my form in my contact.html, and I have 1 php file with all the code for the form, and a javascript folder with all the jquery files. I think for me is knowing where to add that piece code on the form, php file or javascript files??

    P.S I am also using a opensource CMS, so maybe that's why it's not working properly

    Thanks
     
    Last edited: Sep 8, 2010
    buckmajor, Sep 8, 2010 IP
  8. buckmajor

    buckmajor Active Member

    Messages:
    574
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #8
    Last edited: Sep 8, 2010
    buckmajor, Sep 8, 2010 IP